aboutsummaryrefslogtreecommitdiff
path: root/docs/custom_test.rst
blob: 32d3ec331de0236e75134dea3ab658a361adbcb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
.. _custom-tests:

Add Custom Tests
================

You can add custom test in a file named ``minishell_test.py`` at the root of your project.

Suite
-----

A test suite is a group of related tests.

.. code:: python

   @suite()
   def suite_yoursuitename(test):
       """ a description of the suite """
       test(...)
       test(...)

.. warning::
   your suite function name **must** be prefixed by ``suite_``.

Bonus
+++++

.. code:: python

   @suite(bonus=True)
   def suite_yoursuitename(test):

Test
----

In your suite function you can use the ``test`` function (passed in the suite function argument).

The first argument of ``test`` is passed to the shell (via ``-c``, see :ref:`compatibility`).

.. code:: python

   test("echo bonjour je suis")
   test("cat -e < /etc/shells")
   test("sed 's/sh/foo/g' /etc/shells > notshells")

Setup Command
+++++++++++++

| ``setup`` is a command to run before the test.
| Contrary to the tested command there is no restriction for the ``setup`` command.
| The only requirement for the test setup to be successful is for this command to return a non zero status code.

.. code:: python

   test("cat < somefile", setup="echo file content > somefile")
   test("ls -la",         setup="touch a b c d e")


Compare Files
+++++++++++++

| The ``files`` argument is a list of files to compare against the :ref:`config-reference-shell`..
| Checks if the file exists and the file's content.

.. code:: python

   test("echo bonjour > somefile",                   files=["somefile"])
   test("echo bonjour > a > b > c",                  files=["a", "b", "c"])
   test("echo bonjour > foo ; echo aurevoir >> foo", files=["foo"])

Export Variables
++++++++++++++++

Add environment variable passed to your executable with the ``exports`` dictionnary.

.. note::
   Those variables will be passed **in addition** of the default exports (i.e ``PATH`` and ``TERM``).

.. code:: python

   test("echo $FOO",     exports={"FOO": "foo"})
   test("echo $FOO$BAR", exports={"FOO": "foo", "BAR": "bar"})
   test("echo $SHLVL",   exports={"SHLVL": "100"})

Timeout
+++++++

``timeout`` overwrites the :ref:`default timeout value<config-timeout-test>` of the configuration.

.. code:: python

   test("echo /*/*/*", timeout=60)

Hook
++++

Output
^^^^^^

``hook`` is a function (or list of functions) applied on the output of test after it is done running.

.. code:: python

   def replace_foo_by_bar_hook(output):
       return output.replace("foo", "bar")

   test("echo @@foo foo foo@@", hook=replace_foo_by_bar_hook)
   # initial output:             @@foo foo foo@@
   # after passedf through hook: @@bar bar bar@@

Status Code
^^^^^^^^^^^

``hook_status`` is similar to ``hook`` only it take a status code has it's first argument and return the new status.

.. code:: python

   def reverse_error(status):
       return 0 if status != 0 else 1

   test("cat doesnotexists", hook=reverse_error)
   # status code will be 0 after status hook