aboutsummaryrefslogtreecommitdiff
path: root/main.py
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-06-15 21:05:43 +0200
committerCharles <sircharlesaze@gmail.com>2020-06-15 21:05:43 +0200
commitf928d08ceda74011d387e755c12bf0256a572d8d (patch)
tree3aaec958f55459d228ba089041f128641cf8b5d5 /main.py
parent9682ac83c4b685818bdaf222f813df044b3107fd (diff)
downloadminishell_test-f928d08ceda74011d387e755c12bf0256a572d8d.tar.gz
minishell_test-f928d08ceda74011d387e755c12bf0256a572d8d.tar.bz2
minishell_test-f928d08ceda74011d387e755c12bf0256a572d8d.zip
Added log, verbose, test suites, summary
Diffstat (limited to 'main.py')
-rwxr-xr-xmain.py144
1 files changed, 114 insertions, 30 deletions
diff --git a/main.py b/main.py
index 68e3a5a..290aeb4 100755
--- a/main.py
+++ b/main.py
@@ -4,16 +4,45 @@ import os
import sys
import subprocess
import shutil
+import argparse
import config
+COLOR_RED = "\033[32m"
+COLOR_GREEN = "\033[31m"
+COLOR_CLOSE = "\033[0m"
def green(s):
- return "\033[32m{}\033[0m".format(s)
-
+ return COLOR_RED + s + COLOR_CLOSE
def red(s):
- return "\033[31m{}\033[0m".format(s)
+ return COLOR_GREEN + s + COLOR_CLOSE
+
+def diff(cmd, expected, actual, color=False):
+ ret = """
+WITH: {}
+STATUS: TODO
+{color_expected}----------------------------------------EXPECTED--------------------------------{color_close}
+{}
+{color_actual}----------------------------------------ACTUAL----------------------------------{color_close}
+{}
+================================================================================
+
+"""
+ colors = {}
+ if color:
+ colors = {
+ "color_expected": COLOR_GREEN,
+ "color_actual": COLOR_RED,
+ "color_close": COLOR_CLOSE
+ }
+ else:
+ colors = {
+ "color_expected": "",
+ "color_actual": "",
+ "color_close": ""
+ }
+ return ret.format(cmd, expected, actual, **colors)
def run_sandboxed(program: str, cmd: str) -> str:
@@ -22,62 +51,117 @@ def run_sandboxed(program: str, cmd: str) -> str:
except OSError:
pass
# os.system(self.setup_cmd)
- current_dir = os.getcwd()
- os.chdir(config.SANDBOX_PATH)
- output = ""
- try:
- output = subprocess.check_output([program, "-c", cmd], stderr=subprocess.STDOUT)
- except:
- pass
- os.chdir(current_dir)
+ # TODO: add timeout
+ # https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module
+ process_status = subprocess.run([program, "-c", cmd],
+ text=True,
+ stderr=subprocess.STDOUT,
+ stdout=subprocess.PIPE,
+ cwd=config.SANDBOX_PATH)
+
+ output = process_status.stdout
+
shutil.rmtree(config.SANDBOX_PATH)
return output
+def put_marker(passed):
+ if passed:
+ sys.stdout.write(green(config.PASS_MARKER))
+ else:
+ sys.stdout.write(red(config.FAIL_MARKER))
+ sys.stdout.flush()
+
+
status = 0
ignored_suites = []
suites = {}
current_suite = "default"
+verbose = False
def test(cmd, setup = None, *files):
- global status
-
if current_suite in ignored_suites:
return
expected = run_sandboxed(config.REFERENCE_SHELL_PATH, cmd)
- actual = run_sandboxed(os.path.abspath(os.path.join(config.MINISHELL_DIR, config.MINISHELL_EXEC)), cmd)
+ actual = run_sandboxed(config.MINISHELL_PATH, cmd)
+ global status
if actual != expected:
- sys.stdout.write(red(config.FAIL_MARKER))
status = 1
- else:
- sys.stdout.write(green(config.PASS_MARKER))
- sys.stdout.flush()
+ if not verbose:
+ put_marker(actual == expected)
+ elif actual != expected:
+ print(diff(cmd, expected, actual, True))
if suites.get(current_suite) is None:
suites[current_suite] = []
- suites[current_suite].append((expected, actual))
-
-
-def test_echo():
- test("echo bonjour")
- test("echo je suis charles")
- test("echo je suis charles")
-
+ suites[current_suite].append((cmd, expected, actual))
+
+available_suites = []
+
+def suite(origin):
+ name = origin.__name__[len("suite_"):]
+ available_suites.append(name)
+ def f():
+ global current_suite
+ current_suite = name
+ print(current_suite, end=": ")
+ origin()
+ print()
+ return f
+
+@suite
+def suite_quote():
test("'echo' 'bonjour'")
test("'echo' 'je' 'suis' 'charles'")
- test("'echo' 'je' 'suis' 'charles'")
test('"echo" "bonjour"')
test('"echo" "je" "suis" "charles"')
- test('"echo" "je" "suis" "charles"')
+ test('echo je\'suis\'"charles"')
+ test('echo "je"suis\'charles\'')
+ test('echo \'je\'"suis"charles')
+
+ test('echo "\\""')
+ test('echo "\\$"')
+ test('echo "\\\\"')
+
+@suite
+def suite_echo():
+ test("echo bonjour")
+ test("echo lalalala lalalalal alalalalal alalalala")
+ test("echo " + config.LOREM)
+
+ test("echo -n bonjour")
+ test("echo -n lalalala lalalalal alalalalal alalalala")
+ test("echo -n " + config.LOREM)
def main():
- test_echo()
+ suite_quote()
+ suite_echo()
+
if __name__ == "__main__":
+ parser = argparse.ArgumentParser(description="Minishell test", epilog="Make sure read README.md")
+ parser.add_argument("-v", "--verbose", action="store_true",
+ help="print test result to stdout")
+ parser.add_argument("suites", nargs='*', metavar="suite",
+ help="test suites to run (available suites: {})".format(", ".join(available_suites)))
+ args = parser.parse_args()
+ verbose = args.verbose
+
main()
- sys.exit(status)
+ log_file = open(config.LOG_PATH, "w")
+ print()
+ for suite_name, results in suites.items():
+ print(suite_name, end=": ")
+ pass_total = 0
+ for (cmd, expected, actual) in results:
+ if expected == actual:
+ pass_total += 1
+ else:
+ log_file.write(diff(cmd, expected, actual))
+ print(green(str(pass_total)), green(config.PASS_MARKER), end=" ")
+ print(red(str(len(results) - pass_total)), red(config.FAIL_MARKER))