Skip to content

Commit

Permalink
Merge pull request strictdoc-project#1458 from strictdoc-project/stan…
Browse files Browse the repository at this point in the history
…islaw/query

project_config: "source_root_path" parameter to indicate the files root
  • Loading branch information
stanislaw authored Nov 14, 2023
2 parents 25ed23a + ac20fa7 commit 2b51ccf
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 3 deletions.
4 changes: 1 addition & 3 deletions strictdoc/core/finders/source_files_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions strictdoc/core/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_world():
print("hello world") # noqa: T201
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]

features = [
"REQUIREMENT_TO_SOURCE_TRACEABILITY",
]

source_root_path = "source_root/"
Original file line number Diff line number Diff line change
@@ -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: <a{{.*}}href="../_source_files/file.py.html#REQ-001">

RUN: %cat %S/Output/html/_source_files/file.py.html | filecheck %s --dump-input=fail --check-prefix CHECK-SOURCE-FILE
CHECK-SOURCE-FILE: <a{{.*}}href="../30_path_to_source_files_option/input.html#1-REQ-001"{{.*}}>
CHECK-SOURCE-FILE: <span class="c1"># noqa: T201</span></pre>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]

features = [
"REQUIREMENT_TO_SOURCE_TRACEABILITY",
]

source_root_path = "../source_root/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_world():
print("hello world") # noqa: T201
Original file line number Diff line number Diff line change
@@ -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: <a{{.*}}href="../_source_files/file.py.html#REQ-001">

RUN: %cat %S/root/Output/html/_source_files/file.py.html | filecheck %s --dump-input=fail --check-prefix CHECK-SOURCE-FILE
CHECK-SOURCE-FILE: <a{{.*}}href="../root/input.html#1-REQ-001"{{.*}}>
CHECK-SOURCE-FILE: <span class="c1"># noqa: T201</span></pre>
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello_world():
print("hello world") # noqa: T201
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]

features = [
"REQUIREMENT_TO_SOURCE_TRACEABILITY",
]

source_root_path = "DOES_NOT_EXIST/"
Original file line number Diff line number Diff line change
@@ -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/.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello():
print("Hello") # noqa: T201
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def hello():
print("Hello") # noqa: T201
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]

include_source_paths = [
"src1/**"
]

features = [
"REQUIREMENT_TO_SOURCE_TRACEABILITY"
]

source_root_path = "source_root/"
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2b51ccf

Please sign in to comment.