Spdx-Java-Library icon indicating copy to clipboard operation
Spdx-Java-Library copied to clipboard

Column in DifferenceDescription returned by LicenseCompareHelper.isTextMatchingTemplate is not correct

Open sdheh opened this issue 1 year ago • 1 comments

LicenseCompareHelper.isTextMatchingTemplate has the following code

compareTemplateOutputHandler = new CompareTemplateOutputHandler(removeLineSeparators(removeCommentChars(compareText)));

The column returned if there is a difference in a line from which comment chars were removed is wrong because it refers to the transformed text.

Example:

String licenseText = "// a b";
String template = "a";
System.out.println(LicenseCompareHelper.isTextMatchingTemplate(template, licenseText).getDifferenceMessage());

returns Additional text found after the end of the expected license text starting at line #1 column #3 "b"

sdheh avatar Jun 06 '24 14:06 sdheh

Thanks again @sdheh for the analysis.

Pull requests are welcome - otherwise I'll look into a fix after finishing the SPDX 3.0 upgrade work.

goneall avatar Jun 07 '24 02:06 goneall

@douglasclarke - do you think this is fixed with #310 ?

goneall avatar Apr 16 '25 22:04 goneall

Yes I believe this situation is better. The following test passes:

    @Test
    public void testAdditionalTextAfterWithComments() throws IOException, LicenseTemplateRuleException, LicenseParserException {
        String templateText = "a";
        String compareText = "// a b";
        CompareTemplateOutputHandler templateOutputHandler = new CompareTemplateOutputHandler(compareText);
        SpdxLicenseTemplateHelper.parseTemplate(templateText, templateOutputHandler);

        System.out.println(templateOutputHandler.getDifferences().getDifferenceMessage());

        if (templateOutputHandler.matches()) {
            fail(templateOutputHandler.getDifferences().getDifferenceMessage());
        }
        assertEquals("Additional text found after the end of the expected license text starting at line #1 column #5 \"b\"", templateOutputHandler.getDifferences().getDifferenceMessage());
        assertEquals(1, templateOutputHandler.getDifferences().getDifferences().size());
        LineColumn lineColumn = templateOutputHandler.getDifferences().getDifferences().get(0);
        assertEquals(1, lineColumn.getLine());
        assertEquals(5, lineColumn.getColumn());
        assertEquals(1, lineColumn.getLen());

        String line = compareText.split("\n")[lineColumn.getLine() - 1];
        String failedToken = line.substring(lineColumn.getColumn(), lineColumn.getColumn() + lineColumn.getLen());
        assertEquals("b", failedToken);
    }```

Note that lineColumn has zero based column index 

douglasclarke avatar Apr 17 '25 15:04 douglasclarke

Thanks @douglasclarke - I'll go ahead and close this issue as resolved.

goneall avatar Apr 17 '25 17:04 goneall

One comment I would make is that the fix in #310 is based on the current implementation of LineColumn and src/test/java/org/spdx/licenseTemplate/LicenseTextHelperTest.java of spdx-java-core. I did test with trying to fix LineColumn to be 1 based for both line and column but the fix had many more transitive effects in license compare. Just noting that the fix provided is contextual on the underlying core's current functionality.

douglasclarke avatar Apr 17 '25 17:04 douglasclarke

One comment I would make is that the fix in #310 is based on the current implementation of LineColumn and src/test/java/org/spdx/licenseTemplate/LicenseTextHelperTest.java of spdx-java-core. I did test with trying to fix LineColumn to be 1 based for both line and column but the fix had many more transitive effects in license compare. Just noting that the fix provided is contextual on the underlying core's current functionality.

Perhaps on the next release we can clean up the implementation of LineColumn - the code in license compare is rather complex, so I would expect there to be some additional changes.

goneall avatar Apr 17 '25 18:04 goneall