Skip to content

Commit adc3862

Browse files
committed
Refactor tests
Signed-off-by: yongjunhong <[email protected]>
1 parent 8a66741 commit adc3862

File tree

10 files changed

+176
-145
lines changed

10 files changed

+176
-145
lines changed

tests/pytest/conftest.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@
22
# -*- coding: utf-8 -*-
33
# Copyright (c) 2021 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import shutil
57
import pytest
6-
import subprocess
78

9+
set_up_directories = [
10+
"tests/result/android",
11+
"tests/result/cocoapods",
12+
"tests/result/exclude",
13+
"tests/result/gradle",
14+
"tests/result/gradle2",
15+
"tests/result/helm",
16+
"tests/result/maven1",
17+
"tests/result/maven2",
18+
"tests/result/multi_pypi_npm",
19+
"tests/result/npm1",
20+
"tests/result/npm2",
21+
"tests/result/nuget1",
22+
"tests/result/nuget2",
23+
"tests/result/pub",
24+
"tests/result/pypi",
25+
]
826

9-
@pytest.fixture
10-
def run_command():
11-
def _run_command(command):
12-
result = subprocess.run(command, shell=True, capture_output=True, text=True)
13-
return result.returncode, result.stdout, result.stderr
14-
return _run_command
27+
remove_directories = set_up_directories
28+
29+
30+
@pytest.fixture(scope="session", autouse=True)
31+
def setup_test_result_dir_and_teardown():
32+
print("==============setup==============")
33+
for directory in set_up_directories:
34+
os.makedirs(directory, exist_ok=True)
35+
36+
yield
37+
38+
print("==============tearDown==============")
39+
for directory in remove_directories:
40+
shutil.rmtree(directory)

tests/pytest/package_manager/test_android.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,28 @@
44
# SPDX-License-Identifier: Apache-2.0
55
import os
66
import pytest
7-
8-
UBUNTU_COMMANDS = [
9-
"fosslight_dependency -p tests/test_android -o tests/result/android -m android"
10-
]
7+
import subprocess
118

129
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
13-
INPUT_PATH = os.path.join("tests", "test_android", "sunflower")
14-
OUTPUT_PATH = os.path.join("tests", "result", "android")
15-
16-
WINDOW_COMMANDS = [f"{DIST_PATH} -p {INPUT_PATH} -o {OUTPUT_PATH}"]
1710

1811

12+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
13+
("tests/test_android", "tests/result/android", "-m android")
14+
])
1915
@pytest.mark.ubuntu
20-
def test_ubuntu(run_command):
21-
for command in UBUNTU_COMMANDS:
22-
return_code, stdout, stderr = run_command(command)
23-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
16+
def test_ubuntu(input_path, output_path, extra_args):
17+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
18+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
19+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
20+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
2421

2522

23+
@pytest.mark.parametrize("input_path, output_path", [
24+
(os.path.join("tests", "test_android", "sunflower"), os.path.join("tests", "result", "android"))
25+
])
2626
@pytest.mark.windows
27-
def test_windows(run_command):
28-
for command in WINDOW_COMMANDS:
29-
return_code, stdout, stderr = run_command(command)
30-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
27+
def test_windows(input_path, output_path):
28+
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
29+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
30+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
31+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

tests/pytest/package_manager/test_cocoapods.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
# -*- coding: utf-8 -*-
33
# Copyright (c) 2021 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
5+
import os
56
import pytest
6-
7-
UBUNTU_COMMANDS = [
8-
"fosslight_dependency -p tests/test_cocoapods -o tests/result/cocoapods -m cocoapods"
9-
]
7+
import subprocess
108

119

10+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
11+
("tests/test_cocoapods", "tests/result/cocoapods", "-m cocoapods")
12+
])
1213
@pytest.mark.ubuntu
13-
def test_ubuntu(run_command):
14-
for command in UBUNTU_COMMANDS:
15-
return_code, stdout, stderr = run_command(command)
16-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
14+
def test_ubuntu(input_path, output_path, extra_args):
15+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
16+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
17+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
18+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

tests/pytest/package_manager/test_gradle.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,30 @@
44
# SPDX-License-Identifier: Apache-2.0
55
import os
66
import pytest
7-
8-
UBUNTU_COMMANDS = [
9-
"fosslight_dependency -p tests/test_gradle/jib -o tests/result/gradle",
10-
"fosslight_dependency -p tests/test_gradle2 -o tests/result/gradle2"
11-
]
7+
import subprocess
128

139
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
14-
INPUT_PATH = os.path.join("tests", "test_gradle", "jib")
15-
OUTPUT_PATH = os.path.join("tests", "result", "gradle")
16-
INPUT_PATH2 = os.path.join("tests", "test_gradle2")
17-
OUTPUT_PATH2 = os.path.join("tests", "result", "gradle2")
18-
19-
WINDOW_COMMANDS = [
20-
f"{DIST_PATH} -p {INPUT_PATH} -o {OUTPUT_PATH} -m gradle",
21-
f"{DIST_PATH} -p {INPUT_PATH2} -o {OUTPUT_PATH2} -m gradle",
22-
]
2310

2411

12+
@pytest.mark.parametrize("input_path, output_path", [
13+
("tests/test_gradle/jib", "tests/result/gradle"),
14+
("tests/test_gradle2", "tests/result/gradle2")
15+
])
2516
@pytest.mark.ubuntu
26-
def test_ubuntu(run_command):
27-
for command in UBUNTU_COMMANDS:
28-
return_code, stdout, stderr = run_command(command)
29-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
17+
def test_ubuntu(input_path, output_path):
18+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
19+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
20+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
21+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
3022

3123

24+
@pytest.mark.parametrize("input_path, output_path", [
25+
(os.path.join("tests", "test_gradle", "jib"), os.path.join("tests", "result", "gradle")),
26+
(os.path.join("tests", "test_gradle2"), os.path.join("tests", "result", "gradle2"))
27+
])
3228
@pytest.mark.windows
33-
def test_windows(run_command):
34-
for command in WINDOW_COMMANDS:
35-
return_code, stdout, stderr = run_command(command)
36-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
29+
def test_windows(input_path, output_path):
30+
command = f"{DIST_PATH} -p {input_path} -o {output_path} -m gradle"
31+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
32+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
33+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

tests/pytest/package_manager/test_helm.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
# -*- coding: utf-8 -*-
33
# Copyright (c) 2021 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
5+
import os
56
import pytest
6-
7-
UBUNTU_COMMANDS = [
8-
"fosslight_dependency -p tests/test_helm -o tests/result/helm -m helm"
9-
]
7+
import subprocess
108

119

10+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
11+
("tests/test_helm", "tests/result/helm", "-m helm")
12+
])
1213
@pytest.mark.ubuntu
13-
def test_ubuntu(run_command):
14-
for command in UBUNTU_COMMANDS:
15-
return_code, stdout, stderr = run_command(command)
16-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
14+
def test_ubuntu(input_path, output_path, extra_args):
15+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
16+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
17+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
18+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

tests/pytest/package_manager/test_maven.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,29 @@
44
# SPDX-License-Identifier: Apache-2.0
55
import os
66
import pytest
7-
8-
UBUNTU_COMMANDS = [
9-
"fosslight_dependency -p tests/test_maven1/lombok.maven -o tests/result/maven1",
10-
"fosslight_dependency -p tests/test_maven2 -o tests/result/maven2"
11-
]
7+
import subprocess
128

139
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
14-
INPUT_PATH = os.path.join("tests", "test_maven2")
15-
OUTPUT_PATH = os.path.join("tests", "result", "maven2")
16-
17-
WINDOW_COMMANDS = [f"{DIST_PATH} -p {INPUT_PATH} -o {OUTPUT_PATH} -m maven"]
1810

1911

12+
@pytest.mark.parametrize("input_path, output_path", [
13+
("tests/test_maven1/lombok.maven", "tests/result/maven1"),
14+
("tests/test_maven2", "tests/result/maven2")
15+
])
2016
@pytest.mark.ubuntu
21-
def test_ubuntu(run_command):
22-
for command in UBUNTU_COMMANDS:
23-
return_code, stdout, stderr = run_command(command)
24-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
17+
def test_ubuntu(input_path, output_path):
18+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
19+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
20+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
21+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
2522

2623

24+
@pytest.mark.parametrize("input_path, output_path", [
25+
(os.path.join("tests", "test_maven2"), os.path.join("tests", "result", "maven2"))
26+
])
2727
@pytest.mark.windows
28-
def test_windows(run_command):
29-
for command in WINDOW_COMMANDS:
30-
return_code, stdout, stderr = run_command(command)
31-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
28+
def test_windows(input_path, output_path):
29+
command = f"{DIST_PATH} -p {input_path} -o {output_path} -m maven"
30+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
31+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
32+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

tests/pytest/package_manager/test_npm.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
# -*- coding: utf-8 -*-
33
# Copyright (c) 2021 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
5+
import os
56
import pytest
6-
7-
UBUNTU_COMMANDS = [
8-
"fosslight_dependency -p tests/test_npm1 -o tests/result/npm1",
9-
"fosslight_dependency -p tests/test_npm2 -o tests/result/npm2 -m npm"
10-
]
7+
import subprocess
118

129

10+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
11+
("tests/test_npm1", "tests/result/npm1", ""),
12+
("tests/test_npm2", "tests/result/npm2", "-m npm")
13+
])
1314
@pytest.mark.ubuntu
14-
def test_ubuntu(run_command):
15-
for command in UBUNTU_COMMANDS:
16-
return_code, stdout, stderr = run_command(command)
17-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
15+
def test_ubuntu(input_path, output_path, extra_args):
16+
command = f"fosslight_dependency -p {input_path} -o {output_path} {extra_args}"
17+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
18+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
19+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

tests/pytest/package_manager/test_nuget.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,35 @@
44
# SPDX-License-Identifier: Apache-2.0
55
import os
66
import pytest
7+
import subprocess
78

89
UBUNTU_COMMANDS = [
910
"fosslight_dependency -p tests/test_nuget -o tests/result/nuget1",
1011
"fosslight_dependency -p tests/test_nuget2 -o tests/result/nuget2"
1112
]
1213

1314
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
14-
INPUT_PATH = os.path.join("tests", "test_nuget")
15-
OUTPUT_PATH = os.path.join("tests", "result", "nuget1")
16-
INPUT_PATH2 = os.path.join("tests", "test_nuget2")
17-
OUTPUT_PATH2 = os.path.join("tests", "result", "nuget2")
18-
19-
WINDOW_COMMANDS = [
20-
f"{DIST_PATH} -p {INPUT_PATH} -o {OUTPUT_PATH}",
21-
f"{DIST_PATH} -p {INPUT_PATH2} -o {OUTPUT_PATH2}"
22-
]
2315

2416

17+
@pytest.mark.parametrize("input_path, output_path", [
18+
("tests/test_nuget", "tests/result/nuget1"),
19+
("tests/test_nuget2", "tests/result/nuget2")
20+
])
2521
@pytest.mark.ubuntu
26-
def test_ubuntu(run_command):
27-
for command in UBUNTU_COMMANDS:
28-
return_code, stdout, stderr = run_command(command)
29-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
22+
def test_ubuntu(input_path, output_path):
23+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
24+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
25+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
26+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
3027

3128

29+
@pytest.mark.parametrize("input_path, output_path", [
30+
(os.path.join("tests", "test_nuget"), os.path.join("tests", "result", "nuget1")),
31+
(os.path.join("tests", "test_nuget2"), os.path.join("tests", "result", "nuget2"))
32+
])
3233
@pytest.mark.windows
33-
def test_windows(run_command):
34-
for command in WINDOW_COMMANDS:
35-
return_code, stdout, stderr = run_command(command)
36-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
34+
def test_windows(input_path, output_path):
35+
command = f"{DIST_PATH} -p {input_path} -o {output_path}"
36+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
37+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
38+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

tests/pytest/package_manager/test_pub.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,31 @@
44
# SPDX-License-Identifier: Apache-2.0
55
import os
66
import pytest
7-
8-
UBUNTU_COMMANDS = [
9-
"fosslight_dependency -p tests/test_pub -o tests/result/pub",
10-
"fosslight_dependency -p tests/test_exclude -e requirements.txt -o tests/result/exclude"
11-
]
7+
import subprocess
128

139
DIST_PATH = os.path.join(os.environ.get("TOX_PATH"), "dist", "cli.exe")
14-
INPUT_PATH = os.path.join("tests", "test_pub")
15-
OUTPUT_PATH = os.path.join("tests", "result", "pub")
16-
INPUT_PATH2 = os.path.join("tests", "test_exclude")
17-
OUTPUT_PATH2 = os.path.join("tests", "result", "exclude")
18-
19-
WINDOW_COMMANDS = [
20-
f"{DIST_PATH} -p {INPUT_PATH} -o {OUTPUT_PATH}",
21-
f"{DIST_PATH} -p {INPUT_PATH} -o {OUTPUT_PATH} -f opossum",
22-
f"{DIST_PATH} -p {INPUT_PATH} -e requirements.txt -o {OUTPUT_PATH}"
23-
]
2410

2511

12+
@pytest.mark.parametrize("input_path, output_path", [
13+
("tests/test_pub", "tests/result/pub"),
14+
("tests/test_exclude -e requirements.txt", "tests/result/exclude")
15+
])
2616
@pytest.mark.ubuntu
27-
def test_ubuntu(run_command):
28-
for command in UBUNTU_COMMANDS:
29-
return_code, stdout, stderr = run_command(command)
30-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
17+
def test_ubuntu(input_path, output_path):
18+
command = f"fosslight_dependency -p {input_path} -o {output_path}"
19+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
20+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
21+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"
3122

3223

24+
@pytest.mark.parametrize("input_path, output_path, extra_args", [
25+
(os.path.join("tests", "test_pub"), os.path.join("tests", "result", "pub"), ""),
26+
(os.path.join("tests", "test_pub"), os.path.join("tests", "result", "pub"), "-f opossum"),
27+
(os.path.join("tests", "test_exclude") + " -e requirements.txt", os.path.join("tests", "result", "exclude"), "")
28+
])
3329
@pytest.mark.windows
34-
def test_windows(run_command):
35-
for command in WINDOW_COMMANDS:
36-
return_code, stdout, stderr = run_command(command)
37-
assert return_code == 0, f"Command failed: {command}\nstdout: {stdout}\nstderr: {stderr}"
30+
def test_windows(input_path, output_path, extra_args):
31+
command = f"{DIST_PATH} -p {input_path} -o {output_path} {extra_args}"
32+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
33+
assert result.returncode == 0, f"Command failed: {command}\nstdout: {result.stdout}\nstderr: {result.stderr}"
34+
assert any(os.scandir(output_path)), f"Output file does not exist: {output_path}"

0 commit comments

Comments
 (0)