Cli for consistency result#12475
Conversation
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Writing the output to the console is left. |
koppor
left a comment
There was a problem hiding this comment.
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.
| if (outputFormat.isEmpty()) { | ||
| writeFindings(result); | ||
| return; | ||
| } |
There was a problem hiding this comment.
Just assume txt as output format - and write it to System.out - Should be possible with new OutputStreamWriter(System.out).
| 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"))); | ||
| } |
There was a problem hiding this comment.
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"}); |
There was a problem hiding this comment.
Use lower letter for parameters - upper case letters seems so 1990s.
| if (fileName == null) { | ||
| System.out.println(Localization.lang("No file specified for consistency check.")); | ||
| return; | ||
| } |
There was a problem hiding this comment.
Move this after line 305, so that you "exit early" / fail fast.
The "outputformat" is not read in this if - and this not necessary.
| if (outputFormat.isEmpty()) { | ||
| writeFindings(result); | ||
| return; | ||
| } |
There was a problem hiding this comment.
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"}); |
There was a problem hiding this comment.
I renamed the --check-consistency-output-format to --output-format. Is it OK?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
´new OutputStreamWriteris anAutoclosable`. Wrap it with try-with-resources.
| return; | ||
| } | ||
|
|
||
| String outputFileName = fileName.get().substring(0, fileName.get().lastIndexOf('.')) + "_consistency_check." + outputFormat.get().toLowerCase(); |
There was a problem hiding this comment.
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 _)
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Writer implements AutoCloseable and is used inside a try-with-resources block, it will be closed automatically. No need to close it manually.
There was a problem hiding this comment.
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.
- introduce --procelain - try-with-resources - remove unused variable in test - Fix ! vs . (. should be used)
|
I took the liberty to continue the implementation. Introduced |
…u16095/jabref into cliForConsistencyResult
|
try-with should not span nearly the whole method; it should very local. |
koppor
left a comment
There was a problem hiding this comment.
I think, it is good to go now:
- Used inheritance for using
BibliographyConsistencyCheckResultCsvWriterat 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
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
CHANGELOG.mddescribed in a way that is understandable for the average user (if change is visible to the user)