|
| 1 | +"""Tests for the cid2civ script.""" |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +def cid2civ(*args, in_file=None, out_file=None): |
| 8 | + """Run the cid2civ script and check its output.""" |
| 9 | + pwd = os.getcwd() |
| 10 | + |
| 11 | + script = f"{pwd}/scripts/cid2civ" |
| 12 | + data = f"{pwd}/tests/scripts/testdata/cid2civ" |
| 13 | + website = f"{pwd}/tests/scripts/testdata/website" |
| 14 | + |
| 15 | + if in_file: |
| 16 | + args += (f"{data}/{in_file}",) |
| 17 | + |
| 18 | + if "download" in args: |
| 19 | + args += ("--website", f"file://{website}") |
| 20 | + |
| 21 | + result = subprocess.run( |
| 22 | + [script, "--verbose", *args], capture_output=True, text=True |
| 23 | + ) |
| 24 | + |
| 25 | + print(result.stderr, file=sys.stderr) |
| 26 | + print(result.stdout, file=sys.stdout) |
| 27 | + result.check_returncode() |
| 28 | + |
| 29 | + if out_file: |
| 30 | + with open(f"{data}/{out_file}") as f: |
| 31 | + assert result.stdout.strip() == f.read().strip() |
| 32 | + |
| 33 | + return result |
| 34 | + |
| 35 | + |
| 36 | +def test_cid2civ_help(): |
| 37 | + """Test the --help option of the cid2civ script.""" |
| 38 | + result = cid2civ("--help") |
| 39 | + assert "Usage:" in result.stdout |
| 40 | + assert "Commands:" in result.stdout |
| 41 | + assert "Arguments:" in result.stdout |
| 42 | + |
| 43 | + |
| 44 | +def test_cid2civ_download(): |
| 45 | + """Test the download command of the cid2civ script.""" |
| 46 | + cid2civ("download", "aws", out_file="aws.cid") |
| 47 | + cid2civ("download", "azure", out_file="azure.cid") |
| 48 | + cid2civ("download", "google", out_file="google.cid") |
| 49 | + |
| 50 | + |
| 51 | +def test_cid2civ_analyze(): |
| 52 | + """Test the analyze command of the cid2civ script.""" |
| 53 | + cid2civ("analyze", in_file="aws.cid", out_file="aws.txt") |
| 54 | + cid2civ("analyze", in_file="azure.cid", out_file="azure.txt") |
| 55 | + cid2civ("analyze", in_file="google.cid", out_file="google.txt") |
| 56 | + |
| 57 | + |
| 58 | +def test_cid2civ_convert(): |
| 59 | + """Test the convert command of the cid2civ script.""" |
| 60 | + cid2civ("convert", in_file="aws.cid", out_file="aws.civ") |
| 61 | + cid2civ("convert", in_file="azure.cid", out_file="azure.civ") |
| 62 | + cid2civ("convert", in_file="google.cid", out_file="google.civ") |
0 commit comments