Skip to content

Commit

Permalink
Use Paths rather than strings where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Jul 11, 2024
1 parent fbe8d06 commit d2865ed
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 179 deletions.
6 changes: 3 additions & 3 deletions tests/unit/test_activehdl_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _test_compile_project_vhdl(self, standard, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
f"-{standard}",
"-work",
"lib",
Expand Down Expand Up @@ -116,7 +116,7 @@ def test_compile_project_vhdl_extra_flags(self, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"custom",
"flags",
"-2008",
Expand Down Expand Up @@ -322,7 +322,7 @@ def test_supports_vhdl_package_generics_false(self, find_prefix):
self.assertFalse(simif.supports_vhdl_package_generics())

def setUp(self):
self.output_path = str(Path(__file__).parent / "test_activehdl_out")
self.output_path = Path(__file__).parent / "test_activehdl_out"
renew_path(self.output_path)
self.project = Project()
self.cwd = os.getcwd()
Expand Down
34 changes: 17 additions & 17 deletions tests/unit/test_incisive_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,12 @@ def test_simulate_vhdl(self, run_command, find_cds_root_irun, find_cds_root_virt
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -602,12 +602,12 @@ def test_simulate_verilog(self, run_command, find_cds_root_irun, find_cds_root_v
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -681,12 +681,12 @@ def test_simulate_extra_flags(self, run_command, find_cds_root_irun, find_cds_ro
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -719,12 +719,12 @@ def test_simulate_generics_and_parameters(self, run_command, find_cds_root_irun,
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -755,12 +755,12 @@ def test_simulate_hdlvar(self, run_command, find_cds_root_irun, find_cds_root_vi
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -788,7 +788,7 @@ def test_elaborate(self, run_command, find_cds_root_irun, find_cds_root_virtuoso
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
)
]
Expand Down Expand Up @@ -836,7 +836,7 @@ def test_elaborate_fail(self, run_command, find_cds_root_irun, find_cds_root_vir
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
)
]
Expand All @@ -861,12 +861,12 @@ def test_simulate_fail(self, run_command, find_cds_root_irun, find_cds_root_virt
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -899,12 +899,12 @@ def test_simulate_gui(self, run_command, find_cds_root_irun, find_cds_root_virtu
"-f",
Path(elaborate_args_file).name,
],
cwd=str(Path(elaborate_args_file).parent),
cwd=Path(elaborate_args_file).parent,
env=simif.get_env(),
),
mock.call(
[str(Path("prefix") / "irun"), "-f", Path(simulate_args_file).name],
cwd=str(Path(simulate_args_file).parent),
cwd=Path(simulate_args_file).parent,
env=simif.get_env(),
),
]
Expand Down Expand Up @@ -974,7 +974,7 @@ def test_configuration_and_entity_selection(self, find_cds_root_irun, find_cds_r
self.assertEqual(simif._select_vhdl_top(config), "lib.tb_entity:arch") # pylint: disable=protected-access

def setUp(self):
self.output_path = str(Path(__file__).parent / "test_incisive_out")
self.output_path = Path(__file__).parent / "test_incisive_out"
renew_path(self.output_path)
self.project = Project()
self.cwd = os.getcwd()
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_modelsim_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ def test_overwrites_modelsim_ini_file_from_user(self):
def setUp(self):
self.test_path = str(Path(__file__).parent / "test_modelsim_out")

self.output_path = str(Path(self.test_path) / "modelsim")
self.output_path = Path(self.test_path) / "modelsim"
self.prefix_path = str(Path(self.test_path) / "prefix" / "bin")
renew_path(self.test_path)
renew_path(self.output_path)
renew_path(self.prefix_path)
installed_modelsim_ini = str(Path(self.prefix_path) / ".." / "modelsim.ini")
write_file(installed_modelsim_ini, "[Library]")
self.project = Project()
self.cwd = os.getcwd()
self.cwd = Path(os.getcwd())
os.chdir(self.test_path)

def tearDown(self):
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_rivierapro_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_compile_project_vhdl_2019(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-2019",
"-work",
"lib",
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_compile_project_vhdl_2008(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-2008",
"-work",
"lib",
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_compile_project_vhdl_2002(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-2002",
"-work",
"lib",
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_compile_project_vhdl_93(self, _find_prefix, process, check_output):
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"-93",
"-work",
"lib",
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_compile_project_vhdl_extra_flags(self, _find_prefix, process, check_out
str(Path("prefix") / "vcom"),
"-quiet",
"-j",
self.output_path,
str(self.output_path),
"custom",
"flags",
"-2008",
Expand Down Expand Up @@ -385,7 +385,7 @@ def test_compile_project_verilog_define(self, _find_prefix, process, check_outpu
)

def setUp(self):
self.output_path = str(Path(__file__).parent / "test_rivierapro_out")
self.output_path = Path(__file__).parent / "test_rivierapro_out"
renew_path(self.output_path)
self.project = Project()
self.cwd = os.getcwd()
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ def test_get_output_path_on_linux(self):
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (test_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (test_name + "_" + hash_string(test_name)),
)

output_path = "output_path"
test_name = "123._-+"
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (test_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (test_name + "_" + hash_string(test_name)),
)

output_path = "output_path"
Expand All @@ -163,7 +163,7 @@ def test_get_output_path_on_linux(self):
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (safe_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (safe_name + "_" + hash_string(test_name)),
)

def test_get_output_path_on_windows(self):
Expand All @@ -175,15 +175,15 @@ def test_get_output_path_on_windows(self):
with mock.patch("os.environ", new={}):
test_name = "_" * 400
test_output = runner._get_output_path(test_name)
self.assertEqual(len(test_output), 260 - 100 + 1)
self.assertEqual(len(str(test_output)), 260 - 100 + 1)

with mock.patch("os.environ", new={"VUNIT_TEST_OUTPUT_PATH_MARGIN": "-1000"}):
output_path = "output_path"
test_name = "_" * 400
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / (test_name + "_" + hash_string(test_name))),
Path(output_path).resolve() / (test_name + "_" + hash_string(test_name)),
)

with mock.patch("os.environ", new={"VUNIT_SHORT_TEST_OUTPUT_PATHS": ""}):
Expand All @@ -192,7 +192,7 @@ def test_get_output_path_on_windows(self):
test_output = runner._get_output_path(test_name)
self.assertEqual(
test_output,
str(Path(output_path).resolve() / hash_string(test_name)),
Path(output_path).resolve() / hash_string(test_name),
)

@staticmethod
Expand Down
15 changes: 13 additions & 2 deletions vunit/sim_if/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class SimulatorInterface(object): # pylint: disable=too-many-public-methods
supports_colors_in_gui = False

def __init__(self, output_path, gui):
self._output_path = output_path
self._output_path = Path(output_path)
self._gui = gui

@property
def output_path(self):
def output_path(self) -> Path:
return self._output_path

@property
Expand Down Expand Up @@ -319,6 +319,17 @@ def get_env():
Allows inheriting classes to overload this to modify environment variables. Return None for default environment
"""

def get_script_path(self, output_path) -> Path:
"""
Get path for scripts.
Also creates the required directories if they do not exist.
"""
script_path = Path(output_path) / self.name
if not script_path.exists():
script_path.mkdir(parents=True)
return script_path


def isfile(file_name):
"""
Expand Down
Loading

0 comments on commit d2865ed

Please sign in to comment.