Skip to content

Commit 2a39928

Browse files
committed
Define test data path variable for shared use by unit tests
1 parent 3b1299c commit 2a39928

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

compilesketches/tests/test_compilesketches.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import compilesketches
1616
import reportsizetrends
1717

18+
test_data_path = pathlib.PurePath(os.path.dirname(os.path.realpath(__file__)), "testdata")
19+
1820

1921
def get_compilesketches_object(
2022
cli_version=unittest.mock.sentinel.cli_version,
@@ -256,8 +258,7 @@ def test_compile_sketches(mocker, compilation_success_list, expected_success, do
256258
def test_install_arduino_cli(tmpdir, mocker):
257259
cli_version = "1.2.3"
258260
arduino_cli_user_directory_path = pathlib.PurePath("/foo/arduino_cli_user_directory_path")
259-
source_file_path = pathlib.PurePath(os.path.dirname(os.path.realpath(__file__)),
260-
"testdata", "githubevent.json")
261+
source_file_path = test_data_path.joinpath("githubevent.json")
261262
# Create temporary folder
262263
arduino_cli_installation_path = pathlib.PurePath(tmpdir.mkdir("test_install_arduino_cli"))
263264
output_archive_path = arduino_cli_installation_path.joinpath("foo_archive.tar.gz")
@@ -580,8 +581,7 @@ def test_find_sketches(capsys, monkeypatch):
580581

581582
# Test sketch path doesn't exist
582583
compile_sketches = get_compilesketches_object(
583-
sketch_paths="\'\"" + os.path.dirname(os.path.realpath(__file__))
584-
+ "/testdata/HasSketches\" \"" + nonexistent_sketch_path + "\"\'"
584+
sketch_paths="\'\"" + nonexistent_sketch_path + "\"\'"
585585
)
586586
with pytest.raises(expected_exception=SystemExit, match="1"):
587587
compile_sketches.find_sketches()
@@ -590,76 +590,64 @@ def test_find_sketches(capsys, monkeypatch):
590590

591591
# Test sketch path is a sketch file
592592
compile_sketches = get_compilesketches_object(
593-
sketch_paths="\"" + os.path.dirname(os.path.realpath(__file__)) + "/testdata/HasSketches/Sketch1/Sketch1.ino\""
593+
sketch_paths="\"" + str(test_data_path.joinpath("HasSketches", "Sketch1", "Sketch1.ino")) + "\""
594594
)
595595
assert compile_sketches.find_sketches() == [
596-
pathlib.Path(os.path.dirname(os.path.realpath(__file__)), "testdata", "HasSketches", "Sketch1")
596+
test_data_path.joinpath("HasSketches", "Sketch1")
597597
]
598598

599599
# Test sketch path is a non-sketch file
600-
non_sketch_path = os.path.dirname(os.path.realpath(__file__)) + "/testdata/NoSketches/NotSketch/NotSketch.foo"
600+
non_sketch_path = str(test_data_path.joinpath("NoSketches", "NotSketch", "NotSketch.foo"))
601601
compile_sketches = get_compilesketches_object(sketch_paths="\"" + non_sketch_path + "\"")
602602
with pytest.raises(expected_exception=SystemExit, match="1"):
603603
compile_sketches.find_sketches()
604-
assert capsys.readouterr().out.strip() == ("::error::Sketch path: " + str(pathlib.PurePath(non_sketch_path))
605-
+ " is not a sketch")
604+
assert capsys.readouterr().out.strip() == ("::error::Sketch path: " + non_sketch_path + " is not a sketch")
606605

607606
# Test sketch path is a sketch folder
608607
compile_sketches = get_compilesketches_object(
609-
sketch_paths="\"" + os.path.dirname(os.path.realpath(__file__)) + "/testdata/HasSketches/Sketch1\""
608+
sketch_paths="\"" + str(test_data_path.joinpath("HasSketches", "Sketch1")) + "\""
610609
)
611610
assert compile_sketches.find_sketches() == [
612-
pathlib.Path(os.path.dirname(os.path.realpath(__file__)), "testdata", "HasSketches", "Sketch1")
611+
test_data_path.joinpath("HasSketches", "Sketch1")
613612
]
614613

615614
# Test sketch path does contain sketches
616615
compile_sketches = get_compilesketches_object(
617-
sketch_paths="\"" + os.path.dirname(os.path.realpath(__file__)) + "/testdata/HasSketches\"")
616+
sketch_paths="\"" + str(test_data_path.joinpath("HasSketches")) + "\"")
618617
assert compile_sketches.find_sketches() == [
619-
pathlib.Path(os.path.dirname(os.path.realpath(__file__)), "testdata", "HasSketches", "Sketch1"),
620-
pathlib.Path(os.path.dirname(os.path.realpath(__file__)), "testdata", "HasSketches", "Sketch2")
618+
test_data_path.joinpath("HasSketches", "Sketch1"),
619+
test_data_path.joinpath("HasSketches", "Sketch2")
621620
]
622621

623622
# Test sketch path doesn't contain any sketches
624-
no_sketches_path = os.path.dirname(os.path.realpath(__file__)) + "/testdata/NoSketches"
623+
no_sketches_path = str(test_data_path.joinpath("NoSketches"))
625624
compile_sketches = get_compilesketches_object(
626625
sketch_paths="\"" + no_sketches_path + "\"")
627626
with pytest.raises(expected_exception=SystemExit, match="1"):
628627
compile_sketches.find_sketches()
629628
assert capsys.readouterr().out.strip() == ("::error::No sketches were found in "
630-
+ str(pathlib.PurePath(no_sketches_path)))
629+
+ no_sketches_path)
631630

632631

633632
def test_path_is_sketch():
634633
# Sketch file
635-
assert compilesketches.path_is_sketch(
636-
path=pathlib.Path(os.path.dirname(os.path.realpath(__file__)),
637-
"testdata",
638-
"HasSketches",
639-
"Sketch1",
640-
"Sketch1.ino")
641-
) is True
634+
assert compilesketches.path_is_sketch(path=test_data_path.joinpath("HasSketches", "Sketch1", "Sketch1.ino")) is True
642635

643636
# Not a sketch file
644637
assert compilesketches.path_is_sketch(
645-
path=pathlib.Path(os.path.dirname(os.path.realpath(__file__)),
646-
"testdata",
647-
"NoSketches",
648-
"NotSketch",
649-
"NotSketch.foo")
650-
) is False
638+
path=test_data_path.joinpath("NoSketches", "NotSketch", "NotSketch.foo")) is False
651639

652640
# Sketch folder
653641
assert compilesketches.path_is_sketch(
654-
path=pathlib.Path(os.path.dirname(os.path.realpath(__file__)), "testdata", "HasSketches", "Sketch1")) is True
642+
path=test_data_path.joinpath("HasSketches", "Sketch1")) is True
655643

656644
# No files in path
657645
assert compilesketches.path_is_sketch(
658-
path=pathlib.Path(os.path.dirname(os.path.realpath(__file__)), "testdata", "HasSketches")) is False
646+
path=test_data_path.joinpath("HasSketches")) is False
659647

660648
# Not a sketch folder
661649
assert compilesketches.path_is_sketch(
662-
path=pathlib.Path(os.path.dirname(os.path.realpath(__file__)), "testdata", "NoSketches", "NotSketch")) is False
650+
path=test_data_path.joinpath("NoSketches", "NotSketch")) is False
663651

664652

665653
@pytest.mark.parametrize("returncode, expected_success", [(1, False),
@@ -943,7 +931,7 @@ def get_pull(self):
943931

944932
monkeypatch.setenv("GITHUB_REPOSITORY", "fooRepository/fooOwner")
945933
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
946-
monkeypatch.setenv("GITHUB_EVENT_PATH", os.path.dirname(os.path.realpath(__file__)) + "/testdata/githubevent.json")
934+
monkeypatch.setenv("GITHUB_EVENT_PATH", str(test_data_path.joinpath("githubevent.json")))
947935

948936
compile_sketches = get_compilesketches_object()
949937

0 commit comments

Comments
 (0)