From ac20fa776a233235116f539d44c7cfb1c201e456 Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Tue, 14 Nov 2023 21:10:57 +0100 Subject: [PATCH] project_config: "source_root_path" parameter to indicate the files root --- strictdoc/core/finders/source_files_finder.py | 4 +--- strictdoc/core/project_config.py | 21 +++++++++++++++++++ .../30_path_to_source_files_option/input.sdoc | 10 +++++++++ .../source_root/file.py | 2 ++ .../strictdoc.toml | 7 +++++++ .../30_path_to_source_files_option/test.itest | 11 ++++++++++ .../root/input.sdoc | 10 +++++++++ .../root/strictdoc.toml | 7 +++++++ .../source_root/file.py | 2 ++ .../test.itest | 11 ++++++++++ .../input.sdoc | 10 +++++++++ .../source_root/file.py | 2 ++ .../strictdoc.toml | 7 +++++++ .../test.itest | 2 ++ .../input.sdoc | 18 ++++++++++++++++ .../source_root/src1/test1.py | 2 ++ .../source_root/src2/test2.py | 2 ++ .../strictdoc.toml | 11 ++++++++++ .../test.itest | 5 +++++ 19 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/input.sdoc create mode 100644 tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/source_root/file.py create mode 100644 tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/strictdoc.toml create mode 100644 tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/test.itest create mode 100644 tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/input.sdoc create mode 100644 tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/strictdoc.toml create mode 100644 tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/source_root/file.py create mode 100644 tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/test.itest create mode 100644 tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/input.sdoc create mode 100644 tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/source_root/file.py create mode 100644 tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/strictdoc.toml create mode 100644 tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/test.itest create mode 100644 tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/input.sdoc create mode 100644 tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src1/test1.py create mode 100644 tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src2/test2.py create mode 100644 tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/strictdoc.toml create mode 100644 tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/test.itest diff --git a/strictdoc/core/finders/source_files_finder.py b/strictdoc/core/finders/source_files_finder.py index ce55fdbc7..cbc2574ab 100644 --- a/strictdoc/core/finders/source_files_finder.py +++ b/strictdoc/core/finders/source_files_finder.py @@ -21,7 +21,6 @@ class SourceFileType(Enum): @classmethod def create_from_path(cls, path_to_file: str) -> "SourceFileType": - assert os.path.isfile(path_to_file), path_to_file if path_to_file.endswith(".py"): return cls.PYTHON if path_to_file.endswith(".c"): @@ -105,8 +104,7 @@ def find_source_files( found_source_files: List[SourceFile] = [] # TODO: Unify this on the FileTree class level. - # Introduce #mount_directory method? - doctree_root_abs_path = os.getcwd() + doctree_root_abs_path = project_config.source_root_path doctree_root_abs_path = ( os.path.dirname(doctree_root_abs_path) if os.path.isfile(doctree_root_abs_path) diff --git a/strictdoc/core/project_config.py b/strictdoc/core/project_config.py index b8aac1757..11c45f59e 100644 --- a/strictdoc/core/project_config.py +++ b/strictdoc/core/project_config.py @@ -67,12 +67,17 @@ def __init__( server_port: int, include_doc_paths: List[str], exclude_doc_paths: List[str], + source_root_path: str, include_source_paths: List[str], exclude_source_paths: List[str], reqif_profile: str, config_last_update: Optional[datetime.datetime], ): assert isinstance(environment, SDocRuntimeEnvironment) + assert isinstance(source_root_path, str), source_root_path + assert os.path.isdir(source_root_path), source_root_path + assert os.path.isabs(source_root_path), source_root_path + self.environment: SDocRuntimeEnvironment = environment # Settings obtained from the strictdoc.toml config file. @@ -83,6 +88,7 @@ def __init__( self.server_port: int = server_port self.include_doc_paths: List[str] = include_doc_paths self.exclude_doc_paths: List[str] = exclude_doc_paths + self.source_root_path: str = source_root_path self.include_source_paths: List[str] = include_source_paths self.exclude_source_paths: List[str] = exclude_source_paths @@ -116,6 +122,7 @@ def default_config(environment: SDocRuntimeEnvironment): server_port=ProjectConfig.DEFAULT_SERVER_PORT, include_doc_paths=[], exclude_doc_paths=[], + source_root_path=os.getcwd(), include_source_paths=[], exclude_source_paths=[], reqif_profile=ReqIFProfile.P01_SDOC, @@ -285,6 +292,7 @@ def _load_from_dictionary( server_port = ProjectConfig.DEFAULT_SERVER_PORT include_doc_paths = [] exclude_doc_paths = [] + source_root_path = os.getcwd() include_source_paths = [] exclude_source_paths = [] reqif_profile = ReqIFProfile.P01_SDOC @@ -341,6 +349,18 @@ def _load_from_dictionary( ) sys.exit(1) + source_root_path = project_content.get( + "source_root_path", source_root_path + ) + if not os.path.isdir(source_root_path): + print( # noqa: T201 + f"error: strictdoc.toml: 'source_root_path': " + f"Provided path does not exist: " + f"{source_root_path}." + ) + sys.exit(1) + if not os.path.isabs(source_root_path): + source_root_path = os.path.abspath(source_root_path) include_source_paths = project_content.get( "include_source_paths", include_source_paths ) @@ -387,6 +407,7 @@ def _load_from_dictionary( server_port=server_port, include_doc_paths=include_doc_paths, exclude_doc_paths=exclude_doc_paths, + source_root_path=source_root_path, include_source_paths=include_source_paths, exclude_source_paths=exclude_source_paths, reqif_profile=reqif_profile, diff --git a/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/input.sdoc b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/input.sdoc new file mode 100644 index 000000000..ce4dcbb0e --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/input.sdoc @@ -0,0 +1,10 @@ +[DOCUMENT] +TITLE: Hello world doc + +[REQUIREMENT] +UID: REQ-001 +REFS: +- TYPE: File + VALUE: file.py +TITLE: Requirement Title +STATEMENT: Requirement Statement diff --git a/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/source_root/file.py b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/source_root/file.py new file mode 100644 index 000000000..e00fb4153 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/source_root/file.py @@ -0,0 +1,2 @@ +def hello_world(): + print("hello world") # noqa: T201 diff --git a/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/strictdoc.toml b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/strictdoc.toml new file mode 100644 index 000000000..5bcdfe376 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/strictdoc.toml @@ -0,0 +1,7 @@ +[project] + +features = [ + "REQUIREMENT_TO_SOURCE_TRACEABILITY", +] + +source_root_path = "source_root/" diff --git a/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/test.itest b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/test.itest new file mode 100644 index 000000000..03c0818fa --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/30_path_to_source_files_option/test.itest @@ -0,0 +1,11 @@ +RUN: %strictdoc export %S --experimental-enable-file-traceability --output-dir Output | filecheck %s --dump-input=fail +CHECK: Published: Hello world doc + +RUN: %check_exists --file "%S/Output/html/_source_files/file.py.html" + +RUN: %cat %S/Output/html/30_path_to_source_files_option/input.html | filecheck %s --dump-input=fail --check-prefix CHECK-HTML +CHECK-HTML: + +RUN: %cat %S/Output/html/_source_files/file.py.html | filecheck %s --dump-input=fail --check-prefix CHECK-SOURCE-FILE +CHECK-SOURCE-FILE: +CHECK-SOURCE-FILE: # noqa: T201 diff --git a/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/input.sdoc b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/input.sdoc new file mode 100644 index 000000000..ce4dcbb0e --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/input.sdoc @@ -0,0 +1,10 @@ +[DOCUMENT] +TITLE: Hello world doc + +[REQUIREMENT] +UID: REQ-001 +REFS: +- TYPE: File + VALUE: file.py +TITLE: Requirement Title +STATEMENT: Requirement Statement diff --git a/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/strictdoc.toml b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/strictdoc.toml new file mode 100644 index 000000000..249849ce1 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/root/strictdoc.toml @@ -0,0 +1,7 @@ +[project] + +features = [ + "REQUIREMENT_TO_SOURCE_TRACEABILITY", +] + +source_root_path = "../source_root/" diff --git a/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/source_root/file.py b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/source_root/file.py new file mode 100644 index 000000000..e00fb4153 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/source_root/file.py @@ -0,0 +1,2 @@ +def hello_world(): + print("hello world") # noqa: T201 diff --git a/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/test.itest b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/test.itest new file mode 100644 index 000000000..881b4ad03 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/31_relative_path_to_source_files_option/test.itest @@ -0,0 +1,11 @@ +RUN: (cd %S/root && %strictdoc export %S/root --output-dir Output | filecheck %s --dump-input=fail) +CHECK: Published: Hello world doc + +RUN: %check_exists --file "%S/root/Output/html/_source_files/file.py.html" + +RUN: %cat %S/root/Output/html/root/input.html | filecheck %s --dump-input=fail --check-prefix CHECK-HTML +CHECK-HTML: + +RUN: %cat %S/root/Output/html/_source_files/file.py.html | filecheck %s --dump-input=fail --check-prefix CHECK-SOURCE-FILE +CHECK-SOURCE-FILE: +CHECK-SOURCE-FILE: # noqa: T201 diff --git a/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/input.sdoc b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/input.sdoc new file mode 100644 index 000000000..ce4dcbb0e --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/input.sdoc @@ -0,0 +1,10 @@ +[DOCUMENT] +TITLE: Hello world doc + +[REQUIREMENT] +UID: REQ-001 +REFS: +- TYPE: File + VALUE: file.py +TITLE: Requirement Title +STATEMENT: Requirement Statement diff --git a/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/source_root/file.py b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/source_root/file.py new file mode 100644 index 000000000..e00fb4153 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/source_root/file.py @@ -0,0 +1,2 @@ +def hello_world(): + print("hello world") # noqa: T201 diff --git a/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/strictdoc.toml b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/strictdoc.toml new file mode 100644 index 000000000..1db1b0ac6 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/strictdoc.toml @@ -0,0 +1,7 @@ +[project] + +features = [ + "REQUIREMENT_TO_SOURCE_TRACEABILITY", +] + +source_root_path = "DOES_NOT_EXIST/" diff --git a/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/test.itest b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/test.itest new file mode 100644 index 000000000..b80ad7761 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/35_path_to_source_files_does_not_exist/test.itest @@ -0,0 +1,2 @@ +RUN: %expect_exit 1 %strictdoc export %S --experimental-enable-file-traceability --output-dir Output | filecheck %s --dump-input=fail +CHECK: error: strictdoc.toml: 'source_root_path': Provided path does not exist: DOES_NOT_EXIST/. diff --git a/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/input.sdoc b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/input.sdoc new file mode 100644 index 000000000..70803339e --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/input.sdoc @@ -0,0 +1,18 @@ +[DOCUMENT] +TITLE: Hello world doc + +[FREETEXT] +**Hello world** +[/FREETEXT] + +[REQUIREMENT] +UID: REQ-001 +REFS: +- TYPE: File + VALUE: src1/test1.py + +[REQUIREMENT] +UID: REQ-002 +REFS: +- TYPE: File + VALUE: src2/test2.py diff --git a/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src1/test1.py b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src1/test1.py new file mode 100644 index 000000000..7dc5ae431 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src1/test1.py @@ -0,0 +1,2 @@ +def hello(): + print("Hello") # noqa: T201 diff --git a/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src2/test2.py b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src2/test2.py new file mode 100644 index 000000000..7dc5ae431 --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/source_root/src2/test2.py @@ -0,0 +1,2 @@ +def hello(): + print("Hello") # noqa: T201 diff --git a/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/strictdoc.toml b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/strictdoc.toml new file mode 100644 index 000000000..3357c260c --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/strictdoc.toml @@ -0,0 +1,11 @@ +[project] + +include_source_paths = [ + "src1/**" +] + +features = [ + "REQUIREMENT_TO_SOURCE_TRACEABILITY" +] + +source_root_path = "source_root/" diff --git a/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/test.itest b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/test.itest new file mode 100644 index 000000000..8bc7871fb --- /dev/null +++ b/tests/integration/commands/export/html/file_traceability/37_source_root_and_include_source_paths/test.itest @@ -0,0 +1,5 @@ +RUN: %expect_exit 1 %strictdoc export %S --output-dir Output/ | filecheck %s --dump-input=fail + +# Expecting a error because the test2.py is filtered out by the +# "include_source_paths" option. +CHECK: error: Requirement REQ-002 references a file that does not exist: src2/test2.py