diff --git a/api/src/main/java/org/itsallcode/openfasttrace/api/core/ItemStatus.java b/api/src/main/java/org/itsallcode/openfasttrace/api/core/ItemStatus.java index 9a0c73a9..f40ecde8 100644 --- a/api/src/main/java/org/itsallcode/openfasttrace/api/core/ItemStatus.java +++ b/api/src/main/java/org/itsallcode/openfasttrace/api/core/ItemStatus.java @@ -5,6 +5,7 @@ /** * This enumeration represents the different statuses of an item. */ +// [impl->dsn~specification-item~4] public enum ItemStatus { /** @@ -22,7 +23,11 @@ public enum ItemStatus /** * The item is rejected. */ - REJECTED; + REJECTED, + /** + * The item declares an unknown status. + */ + UNKNOWN; /** * Parses the given text and return the matching {@link ItemStatus}. @@ -34,16 +39,33 @@ public enum ItemStatus * in case no matching {@link ItemStatus} is found. */ public static ItemStatus parseString(final String text) + { + final ItemStatus status = parseOrUnknown(text); + if (status != UNKNOWN) + { + return status; + } + throw new IllegalArgumentException( + "Unrecognized specification item status: \"" + text + "\""); + } + + /** + * Parses the given text and returns the matching {@link ItemStatus} or {@link #UNKNOWN}. + * + * @param text + * the text to parse, e.g. {@code "APPROVED"}. + * @return the matching {@link ItemStatus}, or {@link #UNKNOWN} when no known status matches + */ + public static ItemStatus parseOrUnknown(final String text) { for (final ItemStatus value : values()) { - if (text.equalsIgnoreCase(value.toString())) + if (value != UNKNOWN && text.equalsIgnoreCase(value.toString())) { return value; } } - throw new IllegalArgumentException( - "Unrecognized specification item status: \"" + text + "\""); + return UNKNOWN; } @Override diff --git a/api/src/main/java/org/itsallcode/openfasttrace/api/core/SpecificationItem.java b/api/src/main/java/org/itsallcode/openfasttrace/api/core/SpecificationItem.java index 755a6a65..8ec770ef 100644 --- a/api/src/main/java/org/itsallcode/openfasttrace/api/core/SpecificationItem.java +++ b/api/src/main/java/org/itsallcode/openfasttrace/api/core/SpecificationItem.java @@ -6,7 +6,7 @@ * A specification item that requires coverage from other items and provides * coverage for other items. */ -// [impl->dsn~specification-item~3] +// [impl->dsn~specification-item~4] public final class SpecificationItem { private final LocatedSpecificationItemId id; diff --git a/api/src/test/java/org/itsallcode/openfasttrace/api/core/TestSpecificationItem.java b/api/src/test/java/org/itsallcode/openfasttrace/api/core/TestSpecificationItem.java index 2ef7b7fe..9486e5fd 100644 --- a/api/src/test/java/org/itsallcode/openfasttrace/api/core/TestSpecificationItem.java +++ b/api/src/test/java/org/itsallcode/openfasttrace/api/core/TestSpecificationItem.java @@ -17,7 +17,7 @@ class TestSpecificationItem private static final SpecificationItemId COVERED_ID = SpecificationItemId.parseId("feat~covered~2"); private static final SpecificationItemId DEPEND_ON_ID = SpecificationItemId.parseId("req~dependency~3"); - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testBuilderAcceptsLocatedIds() { @@ -30,7 +30,7 @@ void testBuilderAcceptsLocatedIds() () -> assertThat(item.getDependOnIds(), contains(DEPEND_ON_ID))); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testPreservesCompatibilityForUnlocatedIds() { @@ -43,7 +43,7 @@ void testPreservesCompatibilityForUnlocatedIds() () -> assertThat(item.getDependOnIds(), contains(DEPEND_ON_ID))); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testToBuilderPreservesAllValues() { @@ -56,7 +56,7 @@ void testToBuilderPreservesAllValues() assertThat(item.toBuilder().build(), equalTo(item)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testToBuilderDoesNotModifyOriginalItem() { @@ -70,7 +70,7 @@ void testToBuilderDoesNotModifyOriginalItem() () -> assertThat(copy.getCoveredIds(), contains(COVERED_ID, DEPEND_ON_ID))); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testBuilderReplacesCoveredIds() { @@ -80,7 +80,7 @@ void testBuilderReplacesCoveredIds() assertThat(item.getCoveredIds(), contains(COVERED_ID)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testBuilderReplacesDependencyIds() { diff --git a/core/src/test/java/org/itsallcode/openfasttrace/core/TestItemStatus.java b/core/src/test/java/org/itsallcode/openfasttrace/core/TestItemStatus.java index beddce9f..1358d6ef 100644 --- a/core/src/test/java/org/itsallcode/openfasttrace/core/TestItemStatus.java +++ b/core/src/test/java/org/itsallcode/openfasttrace/core/TestItemStatus.java @@ -11,6 +11,7 @@ class TestItemStatus { + // [utest->dsn~specification-item~4] @Test void testParseString() { @@ -21,9 +22,19 @@ void testParseString() assertThat(ItemStatus.parseString("ReJeCtEd"), equalTo(ItemStatus.REJECTED)); } + // [utest->dsn~specification-item~4] @Test void testParseUnknownStringThrowsException() { - assertThrows(IllegalArgumentException.class, () -> ItemStatus.parseString("Unkown")); + final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> ItemStatus.parseString("unknown")); + assertThat(exception.getMessage(), equalTo("Unrecognized specification item status: \"unknown\"")); } -} \ No newline at end of file + + // [utest->dsn~specification-item~4] + @Test + void testParseUnknownStringAsUnknown() + { + assertThat(ItemStatus.parseOrUnknown("in review"), equalTo(ItemStatus.UNKNOWN)); + } +} diff --git a/core/src/test/java/org/itsallcode/openfasttrace/core/TestSpecificationItem.java b/core/src/test/java/org/itsallcode/openfasttrace/core/TestSpecificationItem.java index 3da2ce8b..84601378 100644 --- a/core/src/test/java/org/itsallcode/openfasttrace/core/TestSpecificationItem.java +++ b/core/src/test/java/org/itsallcode/openfasttrace/core/TestSpecificationItem.java @@ -71,7 +71,7 @@ private void assertSimpleItemComplete(final SpecificationItem item) assertThat(item.getComment(), equalTo(COMMENT)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testBuildComplexSpecificationItem() { @@ -118,7 +118,7 @@ void testNeedsCoverageByArtifactType() assertThat(item.needsCoverageByArtifactType(NOT_NEEDED_ARTIFACT_TYPE), equalTo(false)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testNeedsCoverage() { @@ -128,7 +128,7 @@ void testNeedsCoverage() assertThat(builder.build().needsCoverage(), equalTo(true)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testLocationIsNullByDefault() { @@ -178,14 +178,14 @@ void testDependencyBuilderWithIdParts() assertThat(item.getDependOnIds(), equalTo(parseIds("foo~bar~3"))); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testDefaultStatusIsApproved() { assertThat(createTestItemBuilder().build().getStatus(), equalTo(ItemStatus.APPROVED)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testBuildWithStatus() { @@ -193,14 +193,14 @@ void testBuildWithStatus() equalTo(ItemStatus.REJECTED)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testByDefaultTagListIsEmpty() { assertThat(createTestItemBuilder().build().getTags(), emptyIterable()); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testTagBuilder() { @@ -208,7 +208,7 @@ void testTagBuilder() containsInAnyOrder("the_tag")); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testBuildingWithOutIdThrowsExepction() { @@ -216,14 +216,14 @@ void testBuildingWithOutIdThrowsExepction() assertThrows(IllegalStateException.class, builder::build); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testForwardsIsFalseByDefault() { assertThat(createSimpleItem().build().isForwarding(), equalTo(false)); } - // [utest->dsn~specification-item~3] + // [utest->dsn~specification-item~4] @Test void testSetForwards() { diff --git a/doc/changes/changes.md b/doc/changes/changes.md index babdb83c..9198b7e1 100644 --- a/doc/changes/changes.md +++ b/doc/changes/changes.md @@ -6,6 +6,7 @@ nav_order: 9 # Changes +* [4.12.2](changes_4.12.2.md) unreleased * [4.12.1](changes_4.12.1.md) unreleased * [4.12.0](changes_4.12.0.md) * [4.11.0](changes_4.11.0.md) diff --git a/doc/changes/changes_4.12.2.md b/doc/changes/changes_4.12.2.md new file mode 100644 index 00000000..fa8c3c12 --- /dev/null +++ b/doc/changes/changes_4.12.2.md @@ -0,0 +1,7 @@ +# OpenFastTrace 4.12.2, Unreleased on GitHub + +In this release we added a warning if a Markdown or reStructuredText file contain a status that OFT does not support. The status is then registered as `UNKNOWN`. + +## Bug Fixes + +* #646: Warn about unknown status values in Markdown and reStructuredText specifications diff --git a/doc/developer_guide/developer_guide.md b/doc/developer_guide/developer_guide.md index eb9491e0..c49faf9f 100644 --- a/doc/developer_guide/developer_guide.md +++ b/doc/developer_guide/developer_guide.md @@ -26,7 +26,7 @@ To use OpenFastTrace as a dependency in your [Maven](https://maven.apache.org) p org.itsallcode.openfasttrace openfasttrace - 4.12.1 + 4.12.2 compile @@ -38,7 +38,7 @@ To use OpenFastTrace as a dependency in your [Gradle](https://gradle.org/) proje ```groovy dependencies { - compile "org.itsallcode.openfasttrace:openfasttrace:4.12.1" + compile "org.itsallcode.openfasttrace:openfasttrace:4.12.2" } ``` diff --git a/doc/spec/design.md b/doc/spec/design.md index 0e0d73d5..e91684f8 100644 --- a/doc/spec/design.md +++ b/doc/spec/design.md @@ -456,6 +456,21 @@ Needs: impl, utest, itest RST and Markdown share a common underlying parser that operates on a line-by-line basis. +##### Lightweight-Markup Specification-Item Status +`dsn~lightweight-markup.specification-item-status~2` + +The shared line parser imports a `Status:` declaration as the current +specification item's `ItemStatus` when its value is `approved`, `proposed`, +`draft`, or `rejected`. For any other value, it assigns `UNKNOWN` and reports a +warning. + +Covers: + +* `req~specification-item~3` +* `req~report-unknown-lightweight-markup-statuses~1` + +Needs: impl, utest, itest + ##### Markdown Comment Coverage Tags `dsn~markdown.comment-coverage-tags~1` @@ -895,13 +910,14 @@ Needs: impl, utest ### Internal Data Structures #### Specification Item -`dsn~specification-item~3` +`dsn~specification-item~4` A `SpecificationItem` consists of the following parts: * ID (`SpecificationItemId`) * Title (`String`, optional) -* Status (`Enum`, optional) +* Status (`ItemStatus`: `APPROVED`, `PROPOSED`, `DRAFT`, `REJECTED`, or + `UNKNOWN`; optional) * Description (`String`, optional) * Rationale (`String`, optional) * Comment (`String`, optional) @@ -918,7 +934,7 @@ See `req~forwarding_needed_coverage~1` for an explanation of the "forwards" fiel Covers: -* `req~specification-item~2` +* `req~specification-item~3` * `req~forwarding_needed_coverage~1` Needs: impl, utest @@ -933,7 +949,7 @@ This allows navigating between specification items. Covers: -* `req~specification-item~2` +* `req~specification-item~3` Needs: impl, utest @@ -948,7 +964,7 @@ A `SpecificationItemId` consists of: Covers: -* `req~specification-item~2` +* `req~specification-item~3` Needs: impl, utest diff --git a/doc/spec/system_requirements.md b/doc/spec/system_requirements.md index 3bd04f5d..3c3aa641 100644 --- a/doc/spec/system_requirements.md +++ b/doc/spec/system_requirements.md @@ -253,7 +253,7 @@ Needs: req ### Anatomy of Specification Items #### Specification Item -`req~specification-item~2` +`req~specification-item~3` A specification item consists of the following parts: @@ -272,7 +272,8 @@ The ID is a unique key through which the specification item can be referenced. I The title is a short summary of the specification item, mostly intended to appear in overview lists. -The status of the item is one of "approved", "proposed", "draft" and "rejected". +If specified, the status of the item must be one of `approved`, `proposed`, +`draft`, or `rejected`. The description contains the normative part of the specification. @@ -383,6 +384,20 @@ Covers: Needs: dsn +##### Report Unknown Lightweight-Markup Statuses +`req~report-unknown-lightweight-markup-statuses~1` + +OFT reports a warning when a Markdown or reStructuredText parser encounters a +`Status:` declaration whose value does not identify a known specification-item +status. + +Covers: + +* [feat~markdown-import~1](#markdown-import) +* [feat~rst-import~1](#restructured-text-rst-import) + +Needs: dsn + #### Markdown Markdown is a simple ASCII-based markup format that is designed to be human-readable in the source. While it can be rendered into HTML, it is perfectly eye-friendly even before rendering. diff --git a/doc/user_guide/use_cases/writing_a_specification.md b/doc/user_guide/use_cases/writing_a_specification.md index 821586e6..fe7b7a4a 100644 --- a/doc/user_guide/use_cases/writing_a_specification.md +++ b/doc/user_guide/use_cases/writing_a_specification.md @@ -107,14 +107,15 @@ Keywords are followed by a colon that separates the keyword from the content. De ##### `Status` -The `Status` keyword takes a single value from `draft`, `proposed`, `approved`, `rejected` to set the status of the item. The status can be used to filter specification items during import (see [Import options](../reference/oft_command_line.md#import-options)). Has to occur before the `Description`, `Rationale` or `Comment`. +The `Status` keyword takes a single value from `draft`, `proposed`, `approved`, `rejected` to set the status of the item. +OFT reports a warning for any other value. +The status can be used to filter specification items during import (see [Import options](../reference/oft_command_line.md#import-options)). Has to occur before the `Description`, `Rationale` or `Comment`. ### A draft spec items `req~draft-item~1` Status: draft This spec item is in the draft state and thus not considered final. - ##### `Covers` diff --git a/importer/lightweightmarkup/src/main/java/org/itsallcode/openfasttrace/importer/lightweightmarkup/AbstractLightWeightMarkupImporter.java b/importer/lightweightmarkup/src/main/java/org/itsallcode/openfasttrace/importer/lightweightmarkup/AbstractLightWeightMarkupImporter.java index 0517d4b5..134801c7 100644 --- a/importer/lightweightmarkup/src/main/java/org/itsallcode/openfasttrace/importer/lightweightmarkup/AbstractLightWeightMarkupImporter.java +++ b/importer/lightweightmarkup/src/main/java/org/itsallcode/openfasttrace/importer/lightweightmarkup/AbstractLightWeightMarkupImporter.java @@ -170,9 +170,17 @@ protected void endItem() /** * Set the specification item status. */ + // [impl->dsn~lightweight-markup.specification-item-status~2] protected void setStatus() { - this.listener.setStatus(ItemStatus.parseString(this.stateMachine.getLastToken())); + final String statusText = this.stateMachine.getLastToken(); + final ItemStatus status = ItemStatus.parseOrUnknown(statusText); + this.listener.setStatus(status); + if (status == ItemStatus.UNKNOWN) + { + LOGGER.warning(() -> "Found unknown status '" + statusText + "' in '" + this.file + ":" + + this.currentContext.lineNumber() + "'."); + } } /** diff --git a/importer/markdown/src/main/java/org/itsallcode/openfasttrace/importer/markdown/MdPattern.java b/importer/markdown/src/main/java/org/itsallcode/openfasttrace/importer/markdown/MdPattern.java index e86ee3c4..3b551ae6 100644 --- a/importer/markdown/src/main/java/org/itsallcode/openfasttrace/importer/markdown/MdPattern.java +++ b/importer/markdown/src/main/java/org/itsallcode/openfasttrace/importer/markdown/MdPattern.java @@ -52,7 +52,7 @@ enum MdPattern + "(?:\\W.*)?"), NOT_EMPTY("([^\n\r]+)"), RATIONALE("Rationale:\\s*"), - STATUS("Status:\\s*(approved|proposed|draft|rejected)\\s*"), + STATUS("Status:\\s*(.*?)\\s*"), TAGS_INT("Tags:\\s*(\\S.*)"), TAGS("Tags:\\s*"), TAG_ENTRY(PatternConstants.UP_TO_3_WHITESPACES + PatternConstants.BULLETS diff --git a/importer/restructuredtext/src/main/java/org/itsallcode/openfasttrace/importer/restructuredtext/RstPattern.java b/importer/restructuredtext/src/main/java/org/itsallcode/openfasttrace/importer/restructuredtext/RstPattern.java index f1197242..43f52179 100644 --- a/importer/restructuredtext/src/main/java/org/itsallcode/openfasttrace/importer/restructuredtext/RstPattern.java +++ b/importer/restructuredtext/src/main/java/org/itsallcode/openfasttrace/importer/restructuredtext/RstPattern.java @@ -50,7 +50,7 @@ enum RstPattern + "(?:\\W.*)?"), NOT_EMPTY("([^\n\r]+)"), RATIONALE("Rationale:\\s*"), - STATUS("Status:\\s*(approved|proposed|draft|rejected)\\s*"), + STATUS("Status:\\s*(.*?)\\s*"), TAGS_INT("Tags:\\s*(\\S.*)"), TAGS("Tags:\\s*"), TAG_ENTRY(PatternConstants.UP_TO_3_WHITESPACES + PatternConstants.BULLETS diff --git a/parent/pom.xml b/parent/pom.xml index 579f6187..e0160cae 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -9,7 +9,7 @@ pom https://github.com/itsallcode/openfasttrace - 4.12.1 + 4.12.2 17 6.1.3 3.6.1 diff --git a/product/src/test/java/org/itsallcode/openfasttrace/cli/CliStarterInternalIT.java b/product/src/test/java/org/itsallcode/openfasttrace/cli/CliStarterInternalIT.java index a75b74e2..b5581d6c 100644 --- a/product/src/test/java/org/itsallcode/openfasttrace/cli/CliStarterInternalIT.java +++ b/product/src/test/java/org/itsallcode/openfasttrace/cli/CliStarterInternalIT.java @@ -341,6 +341,27 @@ void testTraceWithFilteredStatuses(@TempDir final Path tempDir) throws IOExcepti ); } + // [itest->dsn~lightweight-markup.specification-item-status~2] + @Test + void testTraceWithUnknownStatusReportsWarning(@TempDir final Path tempDir) throws IOException { + final Path specFile = tempDir.resolve("spec.md"); + Files.writeString(specFile, """ + `req~unknown-status~1` + Status: in review + """); + + final ExitStatus status = runInternal( + TRACE_COMMAND, specFile.toString(), + OUTPUT_FILE_PARAMETER, this.outputFile.toString(), + REPORT_VERBOSITY_PARAMETER, "all"); + + assertAll( + () -> assertThat(status, equalTo(ExitStatus.OK)), + () -> assertOutputFileExists(true), + () -> assertThat(getStdErr(), containsString("Found unknown status 'in review'")), + () -> assertThat(getOutputFileContent(), containsString("unknown"))); + } + private void assertOutputFileContainsOldMacNewlines() { assertThat("Has old Mac newlines", getOutputFileContent().contains(CARRIAGE_RETURN), equalTo(true)); diff --git a/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/importer/lightweightmarkup/AbstractLightWeightMarkupImporterTest.java b/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/importer/lightweightmarkup/AbstractLightWeightMarkupImporterTest.java index 6bf364d2..967e66df 100644 --- a/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/importer/lightweightmarkup/AbstractLightWeightMarkupImporterTest.java +++ b/testutil/src/main/java/org/itsallcode/openfasttrace/testutil/importer/lightweightmarkup/AbstractLightWeightMarkupImporterTest.java @@ -6,7 +6,12 @@ import static org.itsallcode.openfasttrace.testutil.core.ItemBuilderFactory.item; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -26,6 +31,8 @@ public abstract class AbstractLightWeightMarkupImporterTest { private static final String FILENAME = "the_file.md"; private static final Path PATH = Path.of("/a/b/c.markdown"); + private static final Logger LOGGER = Logger.getLogger( + "org.itsallcode.openfasttrace.importer.lightweightmarkup.AbstractLightWeightMarkupImporter"); private static final String NL = System.lineSeparator(); private static final Pattern TITLE_PLACEHOLDER = Pattern.compile("\\$\\{title\\(\"([^\"]+)\", (\\d+)\\)}"); private final int titleLocationOffset; @@ -628,6 +635,65 @@ void testItemStatsRecognized() .build())); } + // [utest->dsn~lightweight-markup.specification-item-status~2] + @Test + void testUnknownStatusIsRetainedAndLogsWarning() + { + final Level originalLevel = LOGGER.getLevel(); + final RecordingLogHandler logHandler = new RecordingLogHandler(); + LOGGER.setLevel(Level.WARNING); + LOGGER.addHandler(logHandler); + try + { + assertImport("status.md", """ + `req~unknown-status~1` + Status: in review + `req~approved-status~1` + Status: approved + """, contains( + item().id("req", "unknown-status", 1).status(ItemStatus.UNKNOWN) + .location("status.md", 1).build(), + item().id("req", "approved-status", 1).status(ItemStatus.APPROVED) + .location("status.md", 3).build())); + } + finally + { + LOGGER.removeHandler(logHandler); + LOGGER.setLevel(originalLevel); + } + assertThat(logHandler.containsWarning("Found unknown status 'in review'"), equalTo(true)); + } + + private static final class RecordingLogHandler extends Handler + { + private final List records = new ArrayList<>(); + + @Override + public void publish(final LogRecord logRecord) + { + this.records.add(logRecord); + } + + @Override + public void flush() + { + // Nothing to flush. + } + + @Override + public void close() + { + // Nothing to close. + } + + private boolean containsWarning(final String message) + { + return this.records.stream() + .anyMatch(logRecord -> logRecord.getLevel() == Level.WARNING + && logRecord.getMessage().contains(message)); + } + } + // [impl -> dsn~md.specification-item-id-format~4] with UTF-8 @Test void testItemIdSupportsUTF8Characaters()