aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-11 22:20:15 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-11 22:20:15 +0200
commit958c410ba8b621a8a4d8caf04012028e7f151e0f (patch)
treee63465450b581afd2b631510e00b643efc5af9e7 /src/test
parent2ffd95c295b117053f6e5430ba3ccc72beb224aa (diff)
downloadminishell_test-958c410ba8b621a8a4d8caf04012028e7f151e0f.tar.gz
minishell_test-958c410ba8b621a8a4d8caf04012028e7f151e0f.tar.bz2
minishell_test-958c410ba8b621a8a4d8caf04012028e7f151e0f.zip
Added comments
Diffstat (limited to 'src/test')
-rw-r--r--src/test/captured.py9
-rw-r--r--src/test/result.py23
-rw-r--r--src/test/test.py23
3 files changed, 45 insertions, 10 deletions
diff --git a/src/test/captured.py b/src/test/captured.py
index b42352e..f855212 100644
--- a/src/test/captured.py
+++ b/src/test/captured.py
@@ -6,7 +6,7 @@
# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/09/11 12:16:25 by charles #+# #+# #
-# Updated: 2020/09/11 20:08:00 by charles ### ########.fr #
+# Updated: 2020/09/11 20:42:05 by charles ### ########.fr #
# #
# ############################################################################ #
@@ -15,6 +15,12 @@ import config
class Captured:
def __init__(self, output: str, status: int, files_content: [str], is_timeout: bool = False):
+ """Captured class
+ output: captured content
+ status: command status
+ files_content: content of the files altered by the command
+ is_timeout: the command has timed out
+ """
lines = output.split('\n')
for i, l in enumerate(lines):
if l.find(config.REFERENCE_ERROR_BEGIN) == 0:
@@ -37,4 +43,5 @@ class Captured:
@staticmethod
def timeout():
+ """Create a new captured timeout"""
return Captured("", 0, [], is_timeout=True)
diff --git a/src/test/result.py b/src/test/result.py
index 4f46e52..c64f20a 100644
--- a/src/test/result.py
+++ b/src/test/result.py
@@ -6,7 +6,7 @@
# By: charles <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/09/11 12:17:34 by charles #+# #+# #
-# Updated: 2020/09/11 20:06:31 by charles ### ########.fr #
+# Updated: 2020/09/11 22:20:03 by charles ### ########.fr #
# #
# ############################################################################ #
@@ -24,6 +24,12 @@ class Result:
CLOSE_CHARS = "\033[0m"
def __init__(self, cmd: str, file_names: [str], expected: Captured, actual: Captured):
+ """Result class
+ cmd: runned command
+ file_names: names of watched files
+ expected: expected capture
+ actual: actual capture
+ """
self.cmd = cmd
self.file_names = file_names
self.expected = expected
@@ -31,10 +37,8 @@ class Result:
self.colored = True
self.set_colors()
- def toggle_colors(self):
- self.colored = not self.colored
-
def set_colors(self):
+ """Set colors strings on or off based on self.colored"""
if self.colored:
self.color_red = self.RED_CHARS
self.color_green = self.GREEN_CHARS
@@ -62,13 +66,16 @@ class Result:
@property
def passed(self):
+ """Check if the result passed"""
return self.actual == self.expected
@property
def failed(self):
+ """Check if the result failed"""
return not self.passed
def __repr__(self):
+ """Returns a representation of the result based on the verbosity"""
if config.VERBOSE_LEVEL == 0:
return self.green('.') if self.passed else self.red('!')
elif config.VERBOSE_LEVEL == 1:
@@ -83,6 +90,7 @@ class Result:
raise RuntimeError
def put(self):
+ """Print a summary of the result"""
if config.VERBOSE_LEVEL == 2 and self.passed:
return
print(self, end="")
@@ -92,6 +100,7 @@ class Result:
print()
def header(self, title: str) -> str:
+ """Create a one line header with a title"""
return self.bold("|---------------------------------------{:-<40}".format(title))
@property
@@ -106,6 +115,7 @@ class Result:
return self.bold(self.blue(prefix + " " + title))
def file_diff(self, file_name: str, expected: str, actual: str) -> str:
+ """Difference between 2 files"""
if expected == actual:
return ""
return (
@@ -117,6 +127,7 @@ class Result:
)
def files_diff(self):
+ """Difference between watched files"""
return '\n'.join([self.file_diff(n, e, a) for n, e, a in
zip(self.file_names,
self.expected.files_content,
@@ -124,6 +135,7 @@ class Result:
if e != a])
def output_diff(self) -> str:
+ """Difference in command output"""
out = ""
if self.actual.is_timeout:
return "TIMEOUT\n"
@@ -140,12 +152,14 @@ class Result:
return out
def full_diff(self) -> str:
+ """Concat all difference reports"""
return (self.indicator("WITH {}".format(self.escaped_cmd), "|>") + '\n'
+ self.output_diff()
+ self.files_diff()
+ "=" * 80 + '\n')
def cat_e(self, s: str) -> str:
+ """Pass a string through a cat -e like output"""
s = s.replace("\n", "$\n")
if len(s) < 2:
return s
@@ -155,6 +169,7 @@ class Result:
@property
def escaped_cmd(self):
+ """Escape common control characters"""
return (self.cmd
.replace("\t", "\\t")
.replace("\n", "\\n")
diff --git a/src/test/test.py b/src/test/test.py
index 48f05a0..054086a 100644
--- a/src/test/test.py
+++ b/src/test/test.py
@@ -6,7 +6,7 @@
# By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/06/16 21:48:50 by charles #+# #+# #
-# Updated: 2020/09/11 20:00:52 by charles ### ########.fr #
+# Updated: 2020/09/11 20:39:52 by charles ### ########.fr #
# #
# ############################################################################ #
@@ -30,6 +30,15 @@ class Test:
timeout: float = config.TIMEOUT,
signal=None,
hook=None):
+ """Test class
+ cmd: command to execute
+ setup: command to execute before tested command
+ files: files to watch (check content after test)
+ exports: exported variables
+ timeout: maximum amount of time taken by the test
+ signal: signal to send to the test
+ hook: function to execute on the output of the test
+ """
self.cmd = cmd
self.setup = setup
self.files = files
@@ -40,6 +49,7 @@ class Test:
self.hook = hook
def run(self):
+ """Run the test for minishell and the reference shell and print the result out"""
expected = self._run_sandboxed(config.REFERENCE_PATH, "-c")
actual = self._run_sandboxed(config.MINISHELL_PATH, "-c")
s = self.cmd
@@ -52,12 +62,12 @@ class Test:
self.result.put()
def _run_sandboxed(self, shell_path: str, shell_option: str) -> Captured:
- """ run the command in a sandbox environment
-
- capture the output (stdout and stderr)
- capture the content of the watched files after the command is run
+ """ Run the command in a sandbox environment
+ Capture the output (stdout and stderr)
+ Capture the content of the watched files after the command is run
"""
+ # create and setup sandbox
sandbox.create()
if self.setup != "":
try:
@@ -76,6 +86,7 @@ class Test:
"no stderr" if e.stdout is None else e.stdout.decode().strip()))
sys.exit(1)
+ # run the command in the sandbox
process = subprocess.Popen(
[shell_path, shell_option, self.cmd],
stderr=subprocess.STDOUT,
@@ -96,6 +107,7 @@ class Test:
except subprocess.TimeoutExpired:
return Captured.timeout()
+ # get command output
try:
stdout, _ = process.communicate()
output = stdout.decode()
@@ -110,6 +122,7 @@ class Test:
files_content.append(f.read().decode())
except FileNotFoundError:
files_content.append(None)
+
sandbox.remove()
if self.hook is not None:
output = self.hook(output)