-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from Pennycook/compile-command
Improve compile command handling
- Loading branch information
Showing
7 changed files
with
297 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright (C) 2019-2024 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
|
||
def is_source_file(filename: Union[str, os.PathLike]) -> bool: | ||
""" | ||
Parameters | ||
---------- | ||
filename: Union[str, os.Pathlike] | ||
The filename of a potential source file. | ||
Returns | ||
------- | ||
bool | ||
True if the file ends in a recognized extension and False otherwise. | ||
Only files that can be parsed correctly have recognized extensions. | ||
Raises | ||
------ | ||
TypeError | ||
If filename is not a string or Path. | ||
""" | ||
if not (isinstance(filename, str) or isinstance(filename, Path)): | ||
raise TypeError("filename must be a string or Path") | ||
|
||
extension = Path(filename).suffix | ||
supported_extensions = [ | ||
".f90", | ||
".F90", | ||
".f", | ||
".ftn", | ||
".fpp", | ||
".F", | ||
".FOR", | ||
".FTN", | ||
".FPP", | ||
".c", | ||
".h", | ||
".c++", | ||
".cxx", | ||
".cpp", | ||
".cc", | ||
".hpp", | ||
".hxx", | ||
".h++", | ||
".hh", | ||
".inc", | ||
".inl", | ||
".tcc", | ||
".icc", | ||
".ipp", | ||
".cu", | ||
".cuh", | ||
".cl", | ||
".s", | ||
".S", | ||
".asm", | ||
] | ||
return extension in supported_extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright (C) 2019-2024 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (C) 2019-2024 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import unittest | ||
|
||
from codebasin import CompileCommand | ||
|
||
|
||
class TestCompileCommand(unittest.TestCase): | ||
""" | ||
Test CompileCommand class. | ||
""" | ||
|
||
def test_commands_and_arguments(self): | ||
"""Check commands and arguments are not both None""" | ||
|
||
with self.assertRaises(ValueError): | ||
CompileCommand("file.cpp", command=None, arguments=None) | ||
|
||
with self.assertRaises(ValueError): | ||
instance = { | ||
"file": "file.cpp", | ||
} | ||
CompileCommand.from_json(instance) | ||
|
||
def test_command_to_arguments(self): | ||
"""Check commands convert to arguments""" | ||
command = CompileCommand("file.cpp", command="c++ file.cpp") | ||
self.assertEqual(command.arguments, ["c++", "file.cpp"]) | ||
|
||
instance = { | ||
"file": "file.cpp", | ||
"command": "c++ file.cpp", | ||
} | ||
command = CompileCommand.from_json(instance) | ||
self.assertEqual(command.arguments, ["c++", "file.cpp"]) | ||
|
||
def test_arguments_to_command(self): | ||
"""Check arguments convert to command""" | ||
command = CompileCommand("file.cpp", arguments=["c++", "file.cpp"]) | ||
self.assertEqual(str(command), "c++ file.cpp") | ||
|
||
instance = { | ||
"file": "file.cpp", | ||
"arguments": [ | ||
"c++", | ||
"file.cpp", | ||
], | ||
} | ||
command = CompileCommand.from_json(instance) | ||
self.assertEqual(str(command), "c++ file.cpp") | ||
|
||
def test_empty_command(self): | ||
"""Check empty commands are not supported""" | ||
command = CompileCommand("file.cpp", command="") | ||
self.assertFalse(command.is_supported()) | ||
|
||
def test_link_command(self): | ||
"""Check link commands are not supported""" | ||
command = CompileCommand("file.o", command="c++ -o a.out file.o") | ||
self.assertFalse(command.is_supported()) | ||
|
||
def test_valid_command(self): | ||
"""Check valid commands are supported""" | ||
command = CompileCommand("file.cpp", command="c++ file.cpp") | ||
self.assertTrue(command.is_supported()) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Copyright (C) 2019-2024 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright (C) 2019-2024 Intel Corporation | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import unittest | ||
from pathlib import Path | ||
|
||
import codebasin.source as source | ||
|
||
|
||
class TestSource(unittest.TestCase): | ||
""" | ||
Test functionality in the source module. | ||
""" | ||
|
||
def test_is_source_file_string(self): | ||
"""Check source file identification for string filenames""" | ||
self.assertTrue(source.is_source_file("file.cpp")) | ||
self.assertTrue(source.is_source_file("/path/to/file.cpp")) | ||
self.assertFalse(source.is_source_file("file.o")) | ||
self.assertFalse(source.is_source_file("/path/to/file.o")) | ||
|
||
def test_is_source_file_path(self): | ||
"""Check source file identification for Path filenames""" | ||
self.assertTrue(source.is_source_file(Path("file.cpp"))) | ||
self.assertTrue(source.is_source_file(Path("/path/to/file.cpp"))) | ||
self.assertFalse(source.is_source_file(Path("file.o"))) | ||
self.assertFalse(source.is_source_file(Path("/path/to/file.o"))) | ||
|
||
def test_is_source_types(self): | ||
"""Check type validation for is_source""" | ||
with self.assertRaises(TypeError): | ||
source.is_source_file(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |