Skip to content

Commit 887acd6

Browse files
committed
Set GITHUB_WORKSPACE environment variable globally for tests
GITHUB_WORKSPACE is referenced many places. Having it defined globally for the tests avoids the need to set it over and over again, and also the use of many "magic number" paths in the parametrizations.
1 parent 530f423 commit 887acd6

File tree

1 file changed

+69
-57
lines changed

1 file changed

+69
-57
lines changed

compilesketches/tests/test_compilesketches.py

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import compilesketches
1616

17+
os.environ["GITHUB_WORKSPACE"] = "/foo/github-workspace"
18+
1719
test_data_path = pathlib.PurePath(os.path.dirname(os.path.realpath(__file__)), "testdata")
1820

1921

@@ -376,9 +378,7 @@ def test_get_fqbn_platform_dependency(fqbn_arg, expected_platform, expected_addi
376378
([{compilesketches.CompileSketches.dependency_source_url_key: "git://example.com/foo/bar"}], ["repository"]),
377379
]
378380
)
379-
def test_sort_dependency_list(monkeypatch, dependency_list, expected_dependency_type_list):
380-
monkeypatch.setenv("GITHUB_WORKSPACE", "/foo/GitHubWorkspace")
381-
381+
def test_sort_dependency_list(dependency_list, expected_dependency_type_list):
382382
compile_sketches = get_compilesketches_object()
383383

384384
for dependency, expected_dependency_type in zip(dependency_list, expected_dependency_type_list):
@@ -546,7 +546,7 @@ def test_get_manager_dependency_name(dependency, expected_name):
546546
[(False, [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path("Foo")}]),
547547
(True, [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path("Foo")}])]
548548
)
549-
def test_install_platforms_from_path(capsys, monkeypatch, mocker, path_exists, platform_list):
549+
def test_install_platforms_from_path(capsys, mocker, path_exists, platform_list):
550550
class PlatformInstallationPath:
551551
def __init__(self):
552552
self.parent = pathlib.PurePath()
@@ -557,8 +557,6 @@ def __init__(self):
557557
platform_installation_path.platform = pathlib.Path("PlatformInstallationPathName")
558558
symlink_source_path = pathlib.Path("/foo/SymlinkSourcePath")
559559

560-
monkeypatch.setenv("GITHUB_WORKSPACE", "/foo/GitHubWorkspace")
561-
562560
compile_sketches = get_compilesketches_object()
563561

564562
mocker.patch.object(pathlib.Path, "exists", autospec=True, return_value=path_exists)
@@ -771,30 +769,53 @@ def __init__(self):
771769

772770
@pytest.mark.parametrize(
773771
"libraries, expected_manager, expected_path, expected_repository, expected_download",
774-
[("", [], [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.PurePath("/foo/GitHubWorkspace")}],
775-
[], []),
776-
("foo bar", [{compilesketches.CompileSketches.dependency_name_key: "foo"},
777-
{compilesketches.CompileSketches.dependency_name_key: "bar"}],
778-
[{compilesketches.CompileSketches.dependency_source_path_key: pathlib.PurePath("/foo/GitHubWorkspace")}], [], []),
779-
("\"foo\" \"bar\"", [{compilesketches.CompileSketches.dependency_name_key: "foo"},
780-
{compilesketches.CompileSketches.dependency_name_key: "bar"}],
781-
[{compilesketches.CompileSketches.dependency_source_path_key: pathlib.PurePath("/foo/GitHubWorkspace")}], [], []),
782-
("-", [], [], [], []),
772+
[("",
773+
[],
774+
[{compilesketches.CompileSketches.dependency_source_path_key: pathlib.PurePath(os.environ["GITHUB_WORKSPACE"])}],
775+
[],
776+
[]),
777+
("foo bar",
778+
[{compilesketches.CompileSketches.dependency_name_key: "foo"},
779+
{compilesketches.CompileSketches.dependency_name_key: "bar"}],
780+
[{compilesketches.CompileSketches.dependency_source_path_key: pathlib.PurePath(os.environ["GITHUB_WORKSPACE"])}],
781+
[],
782+
[]),
783+
("\"foo\" \"bar\"",
784+
[{compilesketches.CompileSketches.dependency_name_key: "foo"},
785+
{compilesketches.CompileSketches.dependency_name_key: "bar"}],
786+
[{compilesketches.CompileSketches.dependency_source_path_key: pathlib.PurePath(os.environ["GITHUB_WORKSPACE"])}],
787+
[],
788+
[]),
789+
("-",
790+
[],
791+
[],
792+
[],
793+
[]),
783794
("- " + compilesketches.CompileSketches.dependency_name_key + ": foo",
784-
[{compilesketches.CompileSketches.dependency_name_key: "foo"}], [], [], []),
785-
("- " + compilesketches.CompileSketches.dependency_source_path_key + ": /foo/bar", [],
786-
[{compilesketches.CompileSketches.dependency_source_path_key: "/foo/bar"}], [], []),
787-
("- " + compilesketches.CompileSketches.dependency_source_url_key + ": https://example.com/foo.git", [], [],
788-
[{"source-url": "https://example.com/foo.git"}], []),
789-
("- " + compilesketches.CompileSketches.dependency_source_url_key + ": https://example.com/foo.zip", [], [], [],
795+
[{compilesketches.CompileSketches.dependency_name_key: "foo"}],
796+
[],
797+
[],
798+
[]),
799+
("- " + compilesketches.CompileSketches.dependency_source_path_key + ": /foo/bar",
800+
[],
801+
[{compilesketches.CompileSketches.dependency_source_path_key: "/foo/bar"}],
802+
[],
803+
[]),
804+
("- " + compilesketches.CompileSketches.dependency_source_url_key + ": https://example.com/foo.git",
805+
[],
806+
[],
807+
[{"source-url": "https://example.com/foo.git"}],
808+
[]),
809+
("- " + compilesketches.CompileSketches.dependency_source_url_key + ": https://example.com/foo.zip",
810+
[],
811+
[],
812+
[],
790813
[{"source-url": "https://example.com/foo.zip"}])]
791814
)
792-
def test_install_libraries(monkeypatch, mocker, libraries, expected_manager, expected_path, expected_repository,
815+
def test_install_libraries(mocker, libraries, expected_manager, expected_path, expected_repository,
793816
expected_download):
794817
libraries_path = pathlib.Path("/foo/LibrariesPath")
795818

796-
monkeypatch.setenv("GITHUB_WORKSPACE", "/foo/GitHubWorkspace")
797-
798819
compile_sketches = get_compilesketches_object(libraries=libraries)
799820
compile_sketches.libraries_path = libraries_path
800821

@@ -852,23 +873,27 @@ def test_install_libraries_from_library_manager(mocker):
852873
enable_output=run_command_output_level)
853874

854875

855-
@pytest.mark.parametrize("path_exists, library_list, expected_destination_name_list",
856-
[(False, [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path(
857-
"/foo/GitHubWorkspace/Nonexistent")}], []),
858-
(True, [{compilesketches.CompileSketches.dependency_destination_name_key: "FooName",
859-
compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path(
860-
"/foo/GitHubWorkspace/FooLibrary")}], ["FooName"]),
861-
(True, [{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path(
862-
"/foo/GitHubWorkspace")}], ["FooRepoName"]),
863-
(True,
864-
[{compilesketches.CompileSketches.dependency_source_path_key: pathlib.Path(
865-
"/foo/GitHubWorkspace/Bar")}], ["Bar"])])
876+
@pytest.mark.parametrize(
877+
"path_exists, library_list, expected_destination_name_list",
878+
[(False,
879+
[{compilesketches.CompileSketches.dependency_source_path_key: os.environ["GITHUB_WORKSPACE"] + "/Nonexistent"}],
880+
[]),
881+
(True,
882+
[{compilesketches.CompileSketches.dependency_destination_name_key: "FooName",
883+
compilesketches.CompileSketches.dependency_source_path_key: os.environ["GITHUB_WORKSPACE"] + "/FooLibrary"}],
884+
["FooName"]),
885+
(True,
886+
[{compilesketches.CompileSketches.dependency_source_path_key: os.environ["GITHUB_WORKSPACE"]}],
887+
["FooRepoName"]),
888+
(True,
889+
[{compilesketches.CompileSketches.dependency_source_path_key: os.environ["GITHUB_WORKSPACE"] + "/Bar"}],
890+
["Bar"])]
891+
)
866892
def test_install_libraries_from_path(capsys, monkeypatch, mocker, path_exists, library_list,
867893
expected_destination_name_list):
868894
libraries_path = pathlib.Path("/foo/LibrariesPath")
869895
symlink_source_path = pathlib.Path("/foo/SymlinkSourcePath")
870896

871-
monkeypatch.setenv("GITHUB_WORKSPACE", "/foo/GitHubWorkspace")
872897
monkeypatch.setenv("GITHUB_REPOSITORY", "foo/FooRepoName")
873898

874899
compile_sketches = get_compilesketches_object()
@@ -977,11 +1002,9 @@ def test_install_libraries_from_download(mocker):
9771002
compilesketches.install_from_download.assert_has_calls(calls=install_libraries_from_download_calls)
9781003

9791004

980-
def test_find_sketches(capsys, monkeypatch):
1005+
def test_find_sketches(capsys):
9811006
nonexistent_sketch_path = "/foo/NonexistentSketch"
9821007

983-
monkeypatch.setenv("GITHUB_WORKSPACE", "/foo/bar")
984-
9851008
# Test sketch path doesn't exist
9861009
compile_sketches = get_compilesketches_object(
9871010
sketch_paths="\'\"" + nonexistent_sketch_path + "\"\'"
@@ -1055,7 +1078,7 @@ def test_path_is_sketch():
10551078

10561079
@pytest.mark.parametrize("returncode, expected_success", [(1, False),
10571080
(0, True)])
1058-
def test_compile_sketch(capsys, monkeypatch, mocker, returncode, expected_success):
1081+
def test_compile_sketch(capsys, mocker, returncode, expected_success):
10591082
stdout = unittest.mock.sentinel.stdout
10601083
relative_sketch_path = pathlib.PurePath("FooSketch", "FooSketch.ino")
10611084

@@ -1066,8 +1089,6 @@ class CompilationData:
10661089
CompilationData.returncode = returncode
10671090
CompilationData.stdout = stdout
10681091

1069-
monkeypatch.setenv("GITHUB_WORKSPACE", "/foo/bar")
1070-
10711092
compile_sketches = get_compilesketches_object()
10721093

10731094
mocker.patch("compilesketches.CompileSketches.run_arduino_cli_command", autospec=True,
@@ -1092,7 +1113,7 @@ class CompilationData:
10921113

10931114

10941115
@pytest.mark.parametrize("do_size_deltas_report", [True, False])
1095-
def test_get_sketch_report(monkeypatch, mocker, do_size_deltas_report):
1116+
def test_get_sketch_report(mocker, do_size_deltas_report):
10961117
original_git_ref = unittest.mock.sentinel.original_git_ref
10971118
sketch_report_list = [unittest.mock.sentinel.sketch_report_list1, unittest.mock.sentinel.sketch_report_list2]
10981119
sketch = unittest.mock.sentinel.sketch
@@ -1119,8 +1140,6 @@ def __init__(self):
11191140
def checkout(self):
11201141
pass
11211142

1122-
monkeypatch.setenv("GITHUB_WORKSPACE", "/foo/bar")
1123-
11241143
compile_sketches = get_compilesketches_object()
11251144

11261145
mocker.patch("compilesketches.CompileSketches.get_sketch_report_from_output", autospec=True,
@@ -1333,7 +1352,6 @@ def get_pull(self):
13331352
pass
13341353

13351354
monkeypatch.setenv("GITHUB_REPOSITORY", "fooRepository/fooOwner")
1336-
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
13371355
monkeypatch.setenv("GITHUB_EVENT_PATH", str(test_data_path.joinpath("githubevent.json")))
13381356

13391357
compile_sketches = get_compilesketches_object()
@@ -1417,7 +1435,6 @@ def rev_parse(self):
14171435
pass
14181436

14191437
monkeypatch.setenv("GITHUB_REPOSITORY", "fooRepository/fooOwner")
1420-
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
14211438

14221439
sketches_report_path = tmp_path
14231440
fqbn_arg = "arduino:avr:uno"
@@ -1504,12 +1521,10 @@ def test_parse_boolean_input(boolean_input, expected_output):
15041521

15051522
@pytest.mark.parametrize("path, expected_relative_path",
15061523
# Path under workspace
1507-
[("/fooWorkspace/baz", pathlib.PurePath("baz")),
1524+
[(os.environ["GITHUB_WORKSPACE"] + "/baz", pathlib.PurePath("baz")),
15081525
# Path outside workspace
15091526
("/bar/foo", pathlib.Path("/").resolve().joinpath("bar", "foo"))])
1510-
def test_path_relative_to_workspace(monkeypatch, path, expected_relative_path):
1511-
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
1512-
1527+
def test_path_relative_to_workspace(path, expected_relative_path):
15131528
assert compilesketches.path_relative_to_workspace(path=path) == expected_relative_path
15141529
assert compilesketches.path_relative_to_workspace(path=pathlib.PurePath(path)) == expected_relative_path
15151530

@@ -1518,15 +1533,13 @@ def test_path_relative_to_workspace(monkeypatch, path, expected_relative_path):
15181533
# Absolute path
15191534
[("/asdf", pathlib.Path("/").resolve().joinpath("asdf")),
15201535
# Relative path
1521-
("asdf", pathlib.Path("/").resolve().joinpath("fooWorkspace", "asdf")),
1536+
("asdf", pathlib.Path(os.environ["GITHUB_WORKSPACE"]).resolve().joinpath("asdf")),
15221537
# Use of ~
15231538
("~/foo", pathlib.Path.home().joinpath("foo")),
15241539
# Use of ..
15251540
("/foo/bar/../baz", pathlib.Path("/").resolve().joinpath("foo", "baz"))
15261541
])
1527-
def test_absolute_path(monkeypatch, path, expected_absolute_path):
1528-
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
1529-
1542+
def test_absolute_path(path, expected_absolute_path):
15301543
assert compilesketches.absolute_path(path=path) == expected_absolute_path
15311544
assert compilesketches.absolute_path(path=pathlib.PurePath(path)) == expected_absolute_path
15321545

@@ -1536,8 +1549,7 @@ def test_absolute_path(monkeypatch, path, expected_absolute_path):
15361549
[("foo/bar-relative-path", pathlib.PurePath("foo/bar-relative-path")),
15371550
("/foo/bar-absolute-path", pathlib.Path("/").resolve().joinpath("foo", "bar-absolute-path"))]
15381551
)
1539-
def test_absolute_relative_path_conversion(monkeypatch, path, expected_path):
1540-
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
1552+
def test_absolute_relative_path_conversion(path, expected_path):
15411553
assert compilesketches.path_relative_to_workspace(
15421554
path=compilesketches.absolute_path(
15431555
path=path

0 commit comments

Comments
 (0)