Skip to content

Cli for consistency result#12475

Merged
koppor merged 21 commits into
JabRef:mainfrom
priyanshu16095:cliForConsistencyResult
Feb 11, 2025
Merged

Cli for consistency result#12475
koppor merged 21 commits into
JabRef:mainfrom
priyanshu16095:cliForConsistencyResult

Conversation

@priyanshu16095

@priyanshu16095 priyanshu16095 commented Feb 9, 2025

Copy link
Copy Markdown
Contributor

Fixes #11984

This PR adds a CLI to ensure consistency among BibTeX entries, with --check-consistency FILE[FORMAT], supporting txt, csv and console based output.

Mandatory checks

  • I own the copyright of the code submitted and I licence it under the MIT license
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (for UI changes)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

Screenshot (69)

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JUnit tests are failing. In the area "Some checks were not successful", locate "Tests / Unit tests (pull_request)" and click on "Details". This brings you to the test output.

You can then run these tests in IntelliJ to reproduce the failing tests locally. We offer a quick test running howto in the section Final build system checks in our setup guide.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JUnit tests are failing. In the area "Some checks were not successful", locate "Tests / Unit tests (pull_request)" and click on "Details". This brings you to the test output.

You can then run these tests in IntelliJ to reproduce the failing tests locally. We offer a quick test running howto in the section Final build system checks in our setup guide.

@priyanshu16095

Copy link
Copy Markdown
Contributor Author

Writing the output to the console is left.

@koppor koppor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK. Some improvement suggestions inside.

Think of "knobless" tool - a tool should work out of the box without configuration. If I as user say "--check-consistency test.bib", it should just check and output it to System.out.

Comment on lines +322 to +325
if (outputFormat.isEmpty()) {
writeFindings(result);
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just assume txt as output format - and write it to System.out - Should be possible with new OutputStreamWriter(System.out).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment still relevant.

Comment on lines 343 to 357
public void writeFindings(BibliographyConsistencyCheck.Result result) {
System.out.print(Localization.lang("Field Presence Consistency Check Result"));
System.out.print("\n\n");

if (result.entryTypeToResultMap().isEmpty()) {
System.out.println("No errors found.\n");
return;
}

System.out.print("\n");
System.out.print("%s | %s\n".formatted("x", Localization.lang("required field is present")));
System.out.print("%s | %s\n".formatted("o", Localization.lang("optional field is present")));
System.out.print("%s | %s\n".formatted("?", Localization.lang("unknown field is present")));
System.out.print("%s | %s\n".formatted("-", Localization.lang("field is absent")));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this method. Using org.jabref.logic.quality.consistency.BibliographyConsistencyCheckResultTxtWriter is enough.


@Test
void checkConsistencyOutputFormatOption() throws Exception {
CliOptions cli = new CliOptions(new String[] {"--check-consistency", "jabref-authors.bib", "--check-consistency-output-format", "CSV"});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use lower letter for parameters - upper case letters seems so 1990s.

github-actions[bot]

This comment was marked as outdated.

Comment on lines +308 to +311
if (fileName == null) {
System.out.println(Localization.lang("No file specified for consistency check."));
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this after line 305, so that you "exit early" / fail fast.

The "outputformat" is not read in this if - and this not necessary.

Comment on lines +322 to +325
if (outputFormat.isEmpty()) {
writeFindings(result);
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment still relevant.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JUnit tests are failing. In the area "Some checks were not successful", locate "Tests / Unit tests (pull_request)" and click on "Details". This brings you to the test output.

You can then run these tests in IntelliJ to reproduce the failing tests locally. We offer a quick test running howto in the section Final build system checks in our setup guide.


@Test
void checkConsistencyOutputFormatOption() throws Exception {
CliOptions cli = new CliOptions(new String[] {"--check-consistency", "jabref-authors.bib", "--output-format", "csv"});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the --check-consistency-output-format to --output-format. Is it OK?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the one hand, we also have exporters with an output format. On the other hand, their syntax is very oudated.

The whole command line should be reworked.

We need to do sub commands instead of --check-consistency. --> https://clig.dev/#the-basics

But maybe, this is for 7.0 instead of 6.0 :) (But I think, we can do this in 6.0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: This is OK.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we decided a few months ago on picocli as a future replacement for the apache command line parser. We should introduce it with a new major release, and I would then opt for a full review of all cli options.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JUnit tests are failing. In the area "Some checks were not successful", locate "Tests / Unit tests (pull_request)" and click on "Details". This brings you to the test output.

You can then run these tests in IntelliJ to reproduce the failing tests locally. We offer a quick test running howto in the section Final build system checks in our setup guide.

BibliographyConsistencyCheck.Result result = consistencyCheck.check(entries);

if (outputFormat.isEmpty()) {
Writer ouputStreamWriter = new OutputStreamWriter(System.out);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

´new OutputStreamWriteris anAutoclosable`. Wrap it with try-with-resources.

return;
}

String outputFileName = fileName.get().substring(0, fileName.get().lastIndexOf('.')) + "_consistency_check." + outputFormat.get().toLowerCase();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a seperate method.

For ease, I would just call

org.jabref.model.strings.StringUtil#getCorrectFileName(filename, ".consistency-check", outputFormat.get().toLowerCase()

(and accept that . is used instead of _)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is not clear.
I have commented the code. Is that correct?


try (Writer writer = new FileWriter(outputFileName, StandardCharsets.UTF_8)) {
if ("csv".equalsIgnoreCase(outputFormat.get())) {
BibliographyConsistencyCheckResultCsvWriter csvWriter = new BibliographyConsistencyCheckResultCsvWriter(result, writer, entryTypesManager, databaseContext.getMode());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better wrap this in try-with-resources; not sure what happens with the writer; if there are troubles, please open the writers both in the try-with-resources (and accept duplication of writer creation)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writer implements AutoCloseable and is used inside a try-with-resources block, it will be closed automatically. No need to close it manually.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BibliographyConsistencyCheckResultCsvWriter opens a CSVWriter, which uses the Writer.

You close the writer, but not the CSVWriter.

My gut feeling says that the outer writers should be closed - and propagate into the inner writers.

priyanshu16095 and others added 3 commits February 10, 2025 20:56
- introduce --procelain
- try-with-resources
- remove unused variable in test
- Fix ! vs . (. should be used)
@koppor

koppor commented Feb 10, 2025

Copy link
Copy Markdown
Member

I took the liberty to continue the implementation. Introduced --porcelain

@koppor koppor marked this pull request as ready for review February 10, 2025 22:55
@koppor koppor enabled auto-merge February 10, 2025 22:56
github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

@koppor

koppor commented Feb 10, 2025

Copy link
Copy Markdown
Member

try-with should not span nearly the whole method; it should very local.

@koppor koppor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, it is good to go now:

  • Used inheritance for using BibliographyConsistencyCheckResultCsvWriter at the output
  • Did not close System.out
  • Does not output to file - this is the job of the operating system. Read about pipes and filters. Works on both Windows and Linux.
  • Exceptions always need to be logged. (Only in very rare case one omits them whie logging)
  • Exclamation mark in Enlglish is for screaming. Therefore, I changed it to dot.

  • Thankx to your test, I found an issue with closing System.out

@koppor koppor added this pull request to the merge queue Feb 10, 2025
Merged via the queue into JabRef:main with commit f7b2ac0 Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Cli option for BibliographyConsistencyCheckResultTxtWriter

3 participants