Skip to content

Commit ab645f8

Browse files
committed
Update tests to use CodeBase
Since a lot of the functionality driven by the tests has not yet been updated to use Path, a lot of casts between Path and string are introduced here. These casts will eventually be removed, but require changes to other functionality. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 5fc4f8f commit ab645f8

File tree

17 files changed

+225
-262
lines changed

17 files changed

+225
-262
lines changed

tests/basic_asm/test_basic_asm.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
import logging
5-
import os
65
import unittest
6+
from pathlib import Path
77

8-
from codebasin import finder
8+
from codebasin import CodeBase, finder
99
from codebasin.walkers.platform_mapper import PlatformMapper
1010

1111

@@ -15,25 +15,16 @@ class TestBasicAsm(unittest.TestCase):
1515
"""
1616

1717
def setUp(self):
18-
self.rootdir = "./tests/basic_asm/"
18+
self.rootdir = Path(__file__).parent.resolve()
1919
logging.getLogger("codebasin").disabled = True
2020

2121
self.expected_setmap = {frozenset(["CPU"]): 24}
2222

2323
def test_yaml(self):
2424
"""basic_asm/basic_asm.yaml"""
25-
files = ["test.s", "test.S", "test.asm"]
26-
codebase = {
27-
"files": [
28-
os.path.realpath(os.path.join(self.rootdir, f)) for f in files
29-
],
30-
"platforms": ["CPU"],
31-
"exclude_files": set(),
32-
"exclude_patterns": [],
33-
"rootdir": self.rootdir,
34-
}
25+
codebase = CodeBase(self.rootdir)
3526
entries = []
36-
for f in codebase["files"]:
27+
for f in codebase:
3728
entries.append(
3829
{
3930
"file": f,
@@ -43,7 +34,7 @@ def test_yaml(self):
4334
},
4435
)
4536
configuration = {"CPU": entries}
46-
state = finder.find(self.rootdir, codebase, configuration)
37+
state = finder.find(str(self.rootdir), codebase, configuration)
4738
mapper = PlatformMapper(codebase)
4839
setmap = mapper.walk(state)
4940
self.assertDictEqual(
@@ -54,17 +45,9 @@ def test_yaml(self):
5445

5546
def test_ptx(self):
5647
"""basic_asm/basic_asm_ptx.yaml"""
57-
codebase = {
58-
"files": [
59-
os.path.realpath(os.path.join(self.rootdir, "test.ptx")),
60-
],
61-
"platforms": ["GPU"],
62-
"exclude_files": set(),
63-
"exclude_patterns": [],
64-
"rootdir": self.rootdir,
65-
}
48+
codebase = CodeBase(self.rootdir)
6649
entry = {
67-
"file": codebase["files"][0],
50+
"file": str(self.rootdir / "test.ptx"),
6851
"defines": [],
6952
"include_paths": [],
7053
"include_files": [],
@@ -73,7 +56,7 @@ def test_ptx(self):
7356
self.assertRaises(
7457
RuntimeError,
7558
finder.find,
76-
self.rootdir,
59+
str(self.rootdir),
7760
codebase,
7861
configuration,
7962
)

tests/basic_fortran/test_basic_fortran.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
import logging
5-
import os
65
import unittest
6+
from pathlib import Path
77

8-
from codebasin import finder
8+
from codebasin import CodeBase, finder
99
from codebasin.walkers.platform_mapper import PlatformMapper
1010

1111

@@ -15,7 +15,7 @@ class TestBasicFortran(unittest.TestCase):
1515
"""
1616

1717
def setUp(self):
18-
self.rootdir = "./tests/basic_fortran/"
18+
self.rootdir = Path(__file__).parent.resolve()
1919
logging.getLogger("codebasin").disabled = True
2020

2121
self.expected_setmap = {
@@ -26,34 +26,26 @@ def setUp(self):
2626

2727
def test_yaml(self):
2828
"""basic_fortran/basic_fortran.yaml"""
29-
codebase = {
30-
"files": [
31-
os.path.realpath(os.path.join(self.rootdir, "test.f90")),
32-
],
33-
"platforms": ["CPU", "GPU"],
34-
"exclude_files": set(),
35-
"exclude_patterns": [],
36-
"rootdir": self.rootdir,
37-
}
29+
codebase = CodeBase(self.rootdir)
3830
configuration = {
3931
"CPU": [
4032
{
41-
"file": codebase["files"][0],
33+
"file": str(self.rootdir / "test.f90"),
4234
"defines": ["CPU"],
4335
"include_paths": [],
4436
"include_files": [],
4537
},
4638
],
4739
"GPU": [
4840
{
49-
"file": codebase["files"][0],
41+
"file": str(self.rootdir / "test.f90"),
5042
"defines": ["GPU"],
5143
"include_paths": [],
5244
"include_files": [],
5345
},
5446
],
5547
}
56-
state = finder.find(self.rootdir, codebase, configuration)
48+
state = finder.find(str(self.rootdir), codebase, configuration)
5749
mapper = PlatformMapper(codebase)
5850
setmap = mapper.walk(state)
5951
self.assertDictEqual(

tests/build-dir/test_build_dir.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import unittest
88
from pathlib import Path
99

10-
from codebasin import config, finder
10+
from codebasin import CodeBase, config, finder
1111
from codebasin.walkers.platform_mapper import PlatformMapper
1212

1313

@@ -17,7 +17,7 @@ class TestBuildDirectories(unittest.TestCase):
1717
"""
1818

1919
def setUp(self):
20-
self.rootdir = str(Path(__file__).parent)
20+
self.rootdir = Path(__file__).parent.resolve()
2121
logging.getLogger("codebasin").disabled = False
2222

2323
def test_absolute_paths(self):
@@ -26,11 +26,11 @@ def test_absolute_paths(self):
2626
All "file" fields are absolute paths.
2727
"""
2828

29-
source = str(Path(__file__).parent.joinpath("foo.cpp"))
29+
source = self.rootdir / "foo.cpp"
3030

3131
# CBI only understands how to load compilation databases from file.
3232
# For now, create temporary files every time we test.
33-
dir1 = str(Path(__file__).parent.joinpath("build1/"))
33+
dir1 = self.rootdir / "build1/"
3434
build1 = tempfile.NamedTemporaryFile()
3535
json1 = [
3636
{
@@ -42,7 +42,7 @@ def test_absolute_paths(self):
4242
with open(build1.name, "w") as f:
4343
json.dump(json1, f)
4444

45-
dir2 = str(Path(__file__).parent.joinpath("build2/"))
45+
dir2 = self.rootdir / "build2/"
4646
build2 = tempfile.NamedTemporaryFile()
4747
json2 = [
4848
{
@@ -54,13 +54,7 @@ def test_absolute_paths(self):
5454
with open(build2.name, "w") as f:
5555
json.dump(json2, f)
5656

57-
codebase = {
58-
"files": [source],
59-
"platforms": ["one", "two"],
60-
"exclude_files": set(),
61-
"exclude_patterns": [],
62-
"rootdir": self.rootdir,
63-
}
57+
codebase = CodeBase(self.rootdir)
6458

6559
configuration = {}
6660
for name, path in [("one", build1.name), ("two", build2.name)]:
@@ -69,7 +63,7 @@ def test_absolute_paths(self):
6963

7064
expected_setmap = {frozenset(["one", "two"]): 1}
7165

72-
state = finder.find(self.rootdir, codebase, configuration)
66+
state = finder.find(str(self.rootdir), codebase, configuration)
7367
mapper = PlatformMapper(codebase)
7468
setmap = mapper.walk(state)
7569
self.assertDictEqual(setmap, expected_setmap, "Mismatch in setmap")
@@ -80,11 +74,11 @@ def test_empty_platform(self):
8074
This may be a sign that the compilation database has incorrect paths.
8175
"""
8276

83-
source = str(Path(__file__).parent.joinpath("foo.cpp"))
77+
source = self.rootdir / "foo.cpp"
8478

8579
# CBI only understands how to load compilation databases from file.
8680
# For now, create temporary files every time we test.
87-
build = str(Path(__file__).parent.joinpath("build/"))
81+
build = self.rootdir / "build/"
8882
tmp = tempfile.NamedTemporaryFile()
8983
obj = [
9084
{

tests/code-base/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Copyright (C) 2019-2024 Intel Corporation
2+
# SPDX-License-Identifier: BSD-3-Clause

tests/code-base/test_code_base.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Copyright (C) 2019-2024 Intel Corporation
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
import logging
5+
import tempfile
6+
import unittest
7+
import warnings
8+
from pathlib import Path
9+
10+
from codebasin import CodeBase
11+
12+
13+
class TestCodeBase(unittest.TestCase):
14+
"""
15+
Test CodeBase class.
16+
"""
17+
18+
def setUp(self):
19+
logging.getLogger("codebasin").disabled = False
20+
warnings.simplefilter("ignore", ResourceWarning)
21+
22+
# Create a temporary codebase spread across two directories
23+
self.tmp1 = tempfile.TemporaryDirectory()
24+
self.tmp2 = tempfile.TemporaryDirectory()
25+
p1 = Path(self.tmp1.name)
26+
p2 = Path(self.tmp2.name)
27+
open(p1 / "foo.cpp", mode="w").close()
28+
open(p1 / "bar.cpp", mode="w").close()
29+
open(p1 / "baz.h", mode="w").close()
30+
open(p1 / "README.md", mode="w").close()
31+
open(p2 / "qux.cpp", mode="w").close()
32+
open(p2 / "quux.h", mode="w").close()
33+
open(p2 / "README.md", mode="w").close()
34+
35+
def test_constructor(self):
36+
"""Check directories and exclude_patterns are handled correctly"""
37+
path = Path(self.tmp1.name)
38+
codebase = CodeBase(path, exclude_patterns=["*.h"])
39+
self.assertTrue(codebase.directories == [str(path)])
40+
self.assertTrue(codebase.exclude_patterns == ["*.h"])
41+
42+
def test_constructor_validation(self):
43+
"""Check directories and exclude_patterns are valid"""
44+
45+
with self.assertRaises(TypeError):
46+
CodeBase(exclude_patterns="*")
47+
48+
with self.assertRaises(TypeError):
49+
CodeBase(1, "2", 3)
50+
51+
with self.assertRaises(TypeError):
52+
CodeBase(exclude_patterns=[1, "2", 3])
53+
54+
def test_repr(self):
55+
"""Check implementation of __repr__"""
56+
path = Path(self.tmp1.name)
57+
codebase = CodeBase(path, exclude_patterns=["*.h"])
58+
self.assertTrue(
59+
codebase.__repr__(),
60+
f'CodeBase(directories=[{path}], exclude_patterns=[".h"])',
61+
)
62+
63+
def test_contains(self):
64+
"""Check implementation of __contains__"""
65+
p1 = Path(self.tmp1.name)
66+
p2 = Path(self.tmp2.name)
67+
codebase = CodeBase(p1, p2, exclude_patterns=["*.h"])
68+
69+
# Files in the temporary directories should be in the code base.
70+
self.assertTrue(p1 / "foo.cpp" in codebase)
71+
self.assertTrue(p1 / "bar.cpp" in codebase)
72+
self.assertTrue(p2 / "qux.cpp" in codebase)
73+
74+
# Files that match exclude pattern(s) should not be in the code base.
75+
self.assertFalse(p1 / "baz.h" in codebase)
76+
self.assertFalse(p2 / "quux.h" in codebase)
77+
78+
# Files that don't exist should not be in the code base.
79+
self.assertFalse(p1 / "asdf.cpp" in codebase)
80+
self.assertFalse(p2 / "asdf.cpp" in codebase)
81+
82+
# The temporary directories themselves should not be in the code base.
83+
self.assertFalse(p1 in codebase)
84+
self.assertFalse(p2 in codebase)
85+
86+
# Non-source files should not be in the code base.
87+
self.assertFalse(p1 / "README.md" in codebase)
88+
self.assertFalse(p2 / "README.md" in codebase)
89+
90+
def test_iterator(self):
91+
"""Check implementation of __iter__"""
92+
p1 = Path(self.tmp1.name)
93+
p2 = Path(self.tmp2.name)
94+
codebase = CodeBase(p1, p2, exclude_patterns=["*.h"])
95+
96+
files = [f for f in codebase]
97+
expected = [
98+
str(p1 / "bar.cpp"),
99+
str(p1 / "foo.cpp"),
100+
str(p2 / "qux.cpp"),
101+
]
102+
self.assertEqual(files, expected)
103+
104+
105+
if __name__ == "__main__":
106+
unittest.main()

tests/commented_directive/test_commented_directive.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
import logging
5-
import os
65
import unittest
6+
from pathlib import Path
77

8-
from codebasin import finder
8+
from codebasin import CodeBase, finder
99
from codebasin.walkers.platform_mapper import PlatformMapper
1010

1111

@@ -16,7 +16,7 @@ class TestCommentedDirective(unittest.TestCase):
1616
"""
1717

1818
def setUp(self):
19-
self.rootdir = "./tests/commented_directive/"
19+
self.rootdir = Path(__file__).parent.resolve()
2020
logging.getLogger("codebasin").disabled = True
2121

2222
self.expected_setmap = {frozenset(["CPU", "GPU"]): 5}
@@ -30,34 +30,26 @@ def count_children_nodes(self, node):
3030

3131
def test_yaml(self):
3232
"""commented_directive/commented_directive.yaml"""
33-
codebase = {
34-
"files": [
35-
os.path.realpath(os.path.join(self.rootdir, "main.cpp")),
36-
],
37-
"platforms": ["CPU", "GPU"],
38-
"exclude_files": set(),
39-
"exclude_patterns": [],
40-
"rootdir": self.rootdir,
41-
}
33+
codebase = CodeBase(self.rootdir)
4234
configuration = {
4335
"CPU": [
4436
{
45-
"file": codebase["files"][0],
37+
"file": str(self.rootdir / "main.cpp"),
4638
"defines": ["CPU"],
4739
"include_paths": [],
4840
"include_files": [],
4941
},
5042
],
5143
"GPU": [
5244
{
53-
"file": codebase["files"][0],
45+
"file": str(self.rootdir / "main.cpp"),
5446
"defines": ["GPU"],
5547
"include_paths": [],
5648
"include_files": [],
5749
},
5850
],
5951
}
60-
state = finder.find(self.rootdir, codebase, configuration)
52+
state = finder.find(str(self.rootdir), codebase, configuration)
6153
mapper = PlatformMapper(codebase)
6254
setmap = mapper.walk(state)
6355

0 commit comments

Comments
 (0)