Skip to content

Commit

Permalink
👌 Add creator key to needs.json (#1311)
Browse files Browse the repository at this point in the history
To record that the `needs.json` was created by `sphinx_needs` and also the version that created it.
  • Loading branch information
chrisjsewell authored Oct 2, 2024
1 parent 83e4c58 commit aa34901
Show file tree
Hide file tree
Showing 19 changed files with 23 additions and 69 deletions.
13 changes: 2 additions & 11 deletions sphinx_needs/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ def build_needs_json(app: Sphinx, _exception: Exception) -> None:
if isinstance(app.builder, NeedsBuilder):
return

try:
needs_builder = NeedsBuilder(app, env)
except TypeError:
needs_builder = NeedsBuilder(app)
needs_builder.set_environment(env)

needs_builder = NeedsBuilder(app, env)
needs_builder.finish()


Expand Down Expand Up @@ -196,12 +191,8 @@ def build_needs_id_json(app: Sphinx, _exception: Exception) -> None:
# Do not create an additional needs_json for every needs_id, if builder is already "needs_id".
if isinstance(app.builder, NeedsIdBuilder):
return
try:
needs_id_builder = NeedsIdBuilder(app, env)
except TypeError:
needs_id_builder = NeedsIdBuilder(app)
needs_id_builder.set_environment(env)

needs_id_builder = NeedsIdBuilder(app, env)
needs_id_builder.finish()


Expand Down
3 changes: 1 addition & 2 deletions sphinx_needs/needsfile.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"type": "object",
"properties": {
"created": {"type": "string"},
"filters": {"type": "object"},
"filters_amount": {"type": "number"},
"creator": {"type": "object"},
"needs": {
"patternProperties": {
"^.*$": {
Expand Down
8 changes: 6 additions & 2 deletions sphinx_needs/needsfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ def __init__(
)

def update_or_add_version(self, version: str) -> None:
from sphinx_needs.needs import __version__

if version not in self.needs_list["versions"].keys():
self.needs_list["versions"][version] = {
"needs_amount": 0,
"needs": {},
"filters_amount": 0,
"filters": {},
"creator": {
"program": "sphinx_needs",
"version": __version__,
},
}
if self._schema:
self.needs_list["versions"][version]["needs_schema"] = self._schema
Expand Down
3 changes: 0 additions & 3 deletions tests/__snapshots__/test_basic_doc.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
'current_version': '0.1.0',
'versions': dict({
'0.1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'ST_001': dict({
'arch': dict({
Expand Down
6 changes: 0 additions & 6 deletions tests/__snapshots__/test_external.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
'current_version': '1.3',
'versions': dict({
'1.3': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'EXT_REQ_01': dict({
'description': '',
Expand Down Expand Up @@ -473,9 +470,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'EXT_TEST_01': dict({
'arch': dict({
Expand Down
3 changes: 0 additions & 3 deletions tests/__snapshots__/test_need_constraints.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
'current_version': '',
'versions': dict({
'': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'SECURITY_REQ': dict({
'arch': dict({
Expand Down
6 changes: 0 additions & 6 deletions tests/__snapshots__/test_needextend.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
'current_version': '',
'versions': dict({
'': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'REQ_1': dict({
'arch': dict({
Expand Down Expand Up @@ -795,9 +792,6 @@
'current_version': '',
'versions': dict({
'': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'extend_test_003': dict({
'arch': dict({
Expand Down
3 changes: 0 additions & 3 deletions tests/__snapshots__/test_needimport.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'IMPL_01': dict({
'description': 'Incoming links of this spec: :need_incoming:`IMPL_01`.',
Expand Down
9 changes: 0 additions & 9 deletions tests/__snapshots__/test_needs_builder.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'TC_001': dict({
'arch': dict({
Expand Down Expand Up @@ -816,9 +813,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'TC_001': dict({
'description': '',
Expand Down Expand Up @@ -1473,9 +1467,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'TC_001': dict({
'arch': dict({
Expand Down
9 changes: 0 additions & 9 deletions tests/__snapshots__/test_needs_id_builder.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'TC_001': dict({
'arch': dict({
Expand Down Expand Up @@ -87,9 +84,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'TC_NEG_001': dict({
'arch': dict({
Expand Down Expand Up @@ -170,9 +164,6 @@
'current_version': '1.0',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'US_63252': dict({
'arch': dict({
Expand Down
3 changes: 0 additions & 3 deletions tests/__snapshots__/test_service_github.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
'current_version': '1',
'versions': dict({
'1': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'GITHUB_050bec': dict({
'arch': dict({
Expand Down
2 changes: 1 addition & 1 deletion tests/test_basic_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_build_needs(test_app: SphinxTestApp, snapshot):
json_text = Path(app.outdir, "needs.json").read_text()
needs_data = json.loads(json_text)

assert needs_data == snapshot(exclude=props("created", "project"))
assert needs_data == snapshot(exclude=props("created", "project", "creator"))


# Test with needs_id_required=True and missing ids in docs.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_external_json(test_app: SphinxTestApp, snapshot):
app.build()
json_data = Path(app.outdir, "needs.json").read_text()
needs = json.loads(json_data)
assert needs == snapshot(exclude=props("created", "project"))
assert needs == snapshot(exclude=props("created", "project", "creator"))


def test_export_import_round_trip(tmp_path: Path, snapshot):
Expand Down Expand Up @@ -136,4 +136,4 @@ def test_export_import_round_trip(tmp_path: Path, snapshot):

json_data = json.loads(Path(str(app.outdir), "needs.json").read_text("utf8"))

assert json_data == snapshot(exclude=props("created", "project"))
assert json_data == snapshot(exclude=props("created", "project", "creator"))
2 changes: 1 addition & 1 deletion tests/test_need_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_need_constraints(test_app, snapshot):

json_text = Path(app.outdir, "needs.json").read_text()
needs_data = json.loads(json_text)
assert needs_data == snapshot(exclude=props("created", "project"))
assert needs_data == snapshot(exclude=props("created", "project", "creator"))

srcdir = Path(app.srcdir)
out_dir = srcdir / "_build"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_needextend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_doc_needextend_html(test_app: Sphinx, snapshot):
app.build()

needs_data = json.loads(Path(app.outdir, "needs.json").read_text())
assert needs_data == snapshot(exclude=props("created", "project"))
assert needs_data == snapshot(exclude=props("created", "project", "creator"))

index_html = Path(app.outdir, "index.html").read_text()
assert "extend_test_003" in index_html
Expand Down Expand Up @@ -102,4 +102,4 @@ def test_doc_needextend_dynamic(test_app, snapshot):
# assert app._warning.getvalue() == ""

needs_data = json.loads(Path(app.outdir, "needs.json").read_text())
assert needs_data == snapshot(exclude=props("created", "project"))
assert needs_data == snapshot(exclude=props("created", "project", "creator"))
2 changes: 1 addition & 1 deletion tests/test_needimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_import_builder(test_app, snapshot):
app.build()
needs_text = Path(app.outdir, "needs.json").read_text()
needs = json.loads(needs_text)
assert needs == snapshot(exclude=props("created", "project"))
assert needs == snapshot(exclude=props("created", "project", "creator"))


@pytest.mark.parametrize(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_needs_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_doc_needs_builder(test_app, snapshot):
app.build()

needs_list = json.loads(Path(app.outdir, "needs.json").read_text())
assert needs_list == snapshot(exclude=props("created", "project"))
assert needs_list == snapshot(exclude=props("created", "project", "creator"))


@pytest.mark.parametrize(
Expand All @@ -36,7 +36,7 @@ def test_doc_needs_builder_reproducible(test_app, snapshot):
app.build()

needs_list = json.loads(Path(app.outdir, "needs.json").read_text())
assert needs_list == snapshot(exclude=props("project"))
assert needs_list == snapshot(exclude=props("project", "creator"))


@pytest.mark.parametrize(
Expand All @@ -55,7 +55,7 @@ def test_doc_needs_builder_remove_defaults(test_app, snapshot):
app.build()

needs_list = json.loads(Path(app.outdir, "needs.json").read_text())
assert needs_list == snapshot(exclude=props("created", "project"))
assert needs_list == snapshot(exclude=props("created", "project", "creator"))


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_needs_id_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def test_doc_needs_id_builder(test_app, snapshot):
data = {
path.name: json.loads(path.read_text()) for path in needs_id_path.glob("*.json")
}
assert data == snapshot(exclude=props("created", "project"))
assert data == snapshot(exclude=props("created", "project", "creator"))
4 changes: 3 additions & 1 deletion tests/test_service_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def test_build(test_app, snapshot):
assert warnings.splitlines() == expected_warnings

needs_data = json.loads((Path(app.outdir) / "needs.json").read_text("utf8"))
assert needs_data == snapshot(exclude=props("created", "project", "avatar"))
assert needs_data == snapshot(
exclude=props("created", "project", "avatar", "creator")
)


ISSUE_RESPONSE = {
Expand Down

0 comments on commit aa34901

Please sign in to comment.