Skip to content
Closed
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
356a2bc
added assert messages and enabled consistency check test in ArgumentP…
Jenish-1235 Aug 29, 2025
52217d7
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Aug 29, 2025
6b40edb
Merge remote-tracking branch 'origin' into task-get-test-of-consisten…
Jenish-1235 Aug 30, 2025
2712f4b
enable more robust auxImport test and enable checkTestResources befor…
Jenish-1235 Aug 30, 2025
975fea4
Merge branch 'task-get-test-of-consistency-check-running-again' of ht…
Jenish-1235 Aug 30, 2025
cffee73
Merge branch 'JabRef:main' into task-get-test-of-consistency-check-ru…
Jenish-1235 Aug 30, 2025
d9cbc95
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Aug 30, 2025
f28d7b5
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Sep 1, 2025
8bd4ff0
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Sep 2, 2025
52c6ba4
replace IllegalStateException with IOException in checkTestResources
Jenish-1235 Sep 2, 2025
4aa43f7
remove redundant test resources exsistence check
Jenish-1235 Sep 2, 2025
8e2dae4
fix csl-styles submodule changes
Jenish-1235 Sep 2, 2025
0a8a87d
fix csl-locales submodule changes
Jenish-1235 Sep 2, 2025
1f544d9
fix abbrv.jabref.org submodule changes
Jenish-1235 Sep 2, 2025
c30fa2b
remove redundant comments
Jenish-1235 Sep 2, 2025
6dd1b83
Merge branch 'JabRef:main' into task-get-test-of-consistency-check-ru…
Jenish-1235 Sep 3, 2025
b00baec
change checkTestResources test from BeforeAll to ParameterizedTest
Jenish-1235 Sep 3, 2025
da813ef
Merge remote-tracking branch 'origin/task-get-test-of-consistency-che…
Jenish-1235 Sep 3, 2025
fe8b256
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Sep 3, 2025
6ff559e
Merge remote-tracking branch 'origin/task-get-test-of-consistency-che…
Jenish-1235 Sep 3, 2025
ef4209f
fix import checkstyles
Jenish-1235 Sep 3, 2025
1a20187
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Sep 3, 2025
1897db2
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Sep 4, 2025
bd58b14
fix import checkstyles
Jenish-1235 Sep 4, 2025
14da9c1
Merge remote-tracking branch 'origin/task-get-test-of-consistency-che…
Jenish-1235 Sep 4, 2025
1ac04cb
Merge branch 'main' into task-get-test-of-consistency-check-running-a…
Jenish-1235 Sep 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 78 additions & 44 deletions jabkit/src/test/java/org/jabref/cli/ArgumentProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
Expand All @@ -29,8 +31,8 @@
import org.jabref.model.util.DummyFileUpdateMonitor;
import org.jabref.support.BibEntryAssert;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Answers;
Expand All @@ -51,6 +53,23 @@ class ArgumentProcessorTest {

private CommandLine commandLine;

@BeforeAll
static void checkTestResources() {
String[] required = new String[] {"origin.bib", "paper.aux", "ArgumentProcessorTestExportMatches.bib"};
StringBuilder missing = new StringBuilder();
for (String res : required) {
if (ArgumentProcessorTest.class.getResource(res) == null) {
if (missing.length() > 0) {
missing.append(", ");
}
missing.append(res);
}
}
if (missing.length() > 0) {
throw new IllegalStateException("Required test resources missing from classpath: " + missing);
}
}

@BeforeEach()
void setup() {
when(importerPreferences.getCustomImporters()).thenReturn(FXCollections.emptyObservableSet());
Expand All @@ -59,31 +78,40 @@ void setup() {
when(preferences.getExportPreferences()).thenReturn(exportPreferences);
when(preferences.getImporterPreferences()).thenReturn(importerPreferences);
when(preferences.getImportFormatPreferences()).thenReturn(importFormatPreferences);
when(preferences.getSearchPreferences()).thenReturn(new SearchPreferences(
SearchDisplayMode.FILTER,
EnumSet.noneOf(SearchFlags.class),
false,
false,
0,
0,
0));
when(preferences.getSearchPreferences()).thenReturn(new SearchPreferences(SearchDisplayMode.FILTER, EnumSet.noneOf(SearchFlags.class), false, false, 0, 0, 0));

ArgumentProcessor argumentProcessor = new ArgumentProcessor(preferences, entryTypesManager);
commandLine = new CommandLine(argumentProcessor);
}

@Test
void auxImport(@TempDir Path tempDir) throws URISyntaxException {
String fullBib = Path.of(ArgumentProcessorTest.class.getResource("origin.bib").toURI()).toAbsolutePath().toString();
String auxFile = Path.of(ArgumentProcessorTest.class.getResource("paper.aux").toURI()).toAbsolutePath().toString();
void auxImport(@TempDir Path tempDir) throws IOException {
try (InputStream originIs = ArgumentProcessorTest.class.getResourceAsStream("origin.bib");
InputStream auxIs = ArgumentProcessorTest.class.getResourceAsStream("paper.aux")) {
if (originIs == null || auxIs == null) {
throw new IllegalStateException("Required test resources are missing from classpath");
}

Path outputBib = tempDir.resolve("output.bib").toAbsolutePath();
Path fullBib = tempDir.resolve("origin.bib");
Files.copy(originIs, fullBib, StandardCopyOption.REPLACE_EXISTING);

Path auxFilePath = tempDir.resolve("paper.aux");
Files.copy(auxIs, auxFilePath, StandardCopyOption.REPLACE_EXISTING);

List<String> args = List.of("generate-bib-from-aux", "--aux", auxFile, "--input", fullBib, "--output", outputBib.toString());
Path outputBib = tempDir.resolve("output.bib").toAbsolutePath();

commandLine.execute(args.toArray(String[]::new));
List<String> args = List.of("generate-bib-from-aux", "--aux", auxFilePath.toString(), "--input", fullBib.toString(), "--output", outputBib.toString());

assertTrue(Files.exists(outputBib));
int rc = commandLine.execute(args.toArray(String[]::new));
assertEquals(0, rc, "CLI returned non-zero exit code for auxImport: " + rc);

assertTrue(Files.exists(outputBib), "Expected output bib to exist: " + outputBib);

// Validate the produced bib can be parsed and contains at least one entry
BibtexImporter importer = new BibtexImporter(importFormatPreferences, new DummyFileUpdateMonitor());
List<BibEntry> entries = importer.importDatabase(outputBib).getDatabase().getEntries();
assertTrue(entries != null && !entries.isEmpty(), "Expected output bib to contain at least one entry");
Copy link
Member

Choose a reason for hiding this comment

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

Typically, we match List.of(entry, ...) to compare the contents. I can relate that this is not necessary here.

You can keep the idea assertFalse(entries.isEmpty() -- the non-null is implicitly checked.

We typically also do not have assertion text, but since AI always generates it, it is OK to keep.

Copy link
Author

Choose a reason for hiding this comment

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

While, I wrote the initial code as you mentioned as per typical style and upon using AI tools for code review, this changes were suggested. I will be open to learn the nuances of both, and make changes if needed.

Copy link
Author

Choose a reason for hiding this comment

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

Being a new contributor, I wish to learn if this test-style is better in coding practices as entries won't be empty right ?

assertFalse(entries.isEmpty(), "Expected output bib to contain at least one entry");

}
}

@Test
Expand All @@ -92,9 +120,8 @@ void search(@TempDir Path tempDir) throws URISyntaxException, IOException {
String originBibFile = originBib.toAbsolutePath().toString();

Path expectedBib = Path.of(
Objects.requireNonNull(ArgumentProcessorTest.class.getResource("ArgumentProcessorTestExportMatches.bib"))
.toURI()
);
Objects.requireNonNull(ArgumentProcessorTest.class.getResource("ArgumentProcessorTestExportMatches.bib"))
Copy link
Member

Choose a reason for hiding this comment

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

There is also the test at the beginning - not sure why this check is added here again.

.toURI());
Comment on lines -95 to +117
Copy link
Member

Choose a reason for hiding this comment

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

Also remove requireNonNull - to have the tests as clean as the other ones.


BibtexImporter bibtexImporter = new BibtexImporter(importFormatPreferences, new DummyFileUpdateMonitor());
List<BibEntry> expectedEntries = bibtexImporter.importDatabase(expectedBib).getDatabase().getEntries();
Expand All @@ -103,9 +130,10 @@ void search(@TempDir Path tempDir) throws URISyntaxException, IOException {

List<String> args = List.of("search", "--debug", "--query", "author=Einstein", "--input", originBibFile, "--output", outputBib.toString());

commandLine.execute(args.toArray(String[]::new));
int rc = commandLine.execute(args.toArray(String[]::new));
assertEquals(0, rc, "CLI returned non-zero exit code for search: " + rc);

assertTrue(Files.exists(outputBib));
assertTrue(Files.exists(outputBib), "Expected output bib to exist: " + outputBib);
BibEntryAssert.assertEquals(expectedEntries, outputBib, bibtexImporter);
}

Expand All @@ -117,7 +145,7 @@ void convertBibtexToTableRefsAsBib(@TempDir Path tempDir) throws URISyntaxExcept
Path outputHtml = tempDir.resolve("output.html").toAbsolutePath();
String outputHtmlFile = outputHtml.toAbsolutePath().toString();

when(importerPreferences.getCustomImporters()) .thenReturn(FXCollections.emptyObservableSet());
when(importerPreferences.getCustomImporters()).thenReturn(FXCollections.emptyObservableSet());

SaveOrder saveOrder = new SaveOrder(SaveOrder.OrderType.TABLE, List.of());
ExportPreferences exportPreferences = new ExportPreferences(".html", tempDir, saveOrder, List.of());
Expand All @@ -129,30 +157,33 @@ void convertBibtexToTableRefsAsBib(@TempDir Path tempDir) throws URISyntaxExcept

List<String> args = List.of("convert", "--input", originBibFile, "--input-format", "bibtex", "--output", outputHtmlFile, "--output-format", "tablerefsabsbib");

commandLine.execute(args.toArray(String[]::new));
int rc = commandLine.execute(args.toArray(String[]::new));
assertEquals(0, rc, "CLI returned non-zero exit code for convert: " + rc);

assertTrue(Files.exists(outputHtml));
assertTrue(Files.exists(outputHtml), "Expected output html to exist: " + outputHtml);
}

@Test
@Disabled("Does not work in this branch, but we did not touch it. TODO: Fix this")
void checkConsistency() throws URISyntaxException {
Path testBib = Path.of(Objects.requireNonNull(ArgumentProcessorTest.class.getResource("origin.bib")).toURI());
String testBibFile = testBib.toAbsolutePath().toString();

List<String> args = List.of("check-consistency", "--input", testBibFile, "--output-format", "txt");

ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent, true));

int executionResult = commandLine.execute(args.toArray(String[]::new));

String output = outContent.toString();
assertTrue(output.contains("Checking consistency for entry type 1 of 1\n"));
assertTrue(output.contains("Consistency check completed"));
assertEquals(0, executionResult);

System.setOut(System.out);
PrintStream originalOut = System.out;
try {
System.setOut(new PrintStream(outContent, true));

int executionResult = commandLine.execute(args.toArray(String[]::new));

String output = outContent.toString();
assertTrue(output.contains("Checking consistency for entry type 1 of 1\n"), "Unexpected stdout:\n" + output);
assertTrue(output.contains("Consistency check completed"), "Unexpected stdout:\n" + output);
assertEquals(0, executionResult, "Non-zero exit code for check-consistency: " + executionResult + "\nstdout:\n" + output);
} finally {
System.setOut(originalOut);
}
}

@Test
Expand All @@ -164,14 +195,17 @@ void checkConsistencyPorcelain() throws URISyntaxException {
List<String> args = List.of("check-consistency", "--input", testBibFile, "--porcelain");

ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

int executionResult = commandLine.execute(args.toArray(String[]::new));

String output = outContent.toString();
assertEquals("", output);
assertEquals(0, executionResult);

System.setOut(System.out);
PrintStream originalOut = System.out;
try {
System.setOut(new PrintStream(outContent, true));

int executionResult = commandLine.execute(args.toArray(String[]::new));

String output = outContent.toString();
assertEquals("", output.trim(), "Unexpected stdout for porcelain check; content:\n" + output);
assertEquals(0, executionResult, "Non-zero exit code for check-consistency --porcelain: " + executionResult + "\nstdout:\n" + output);
} finally {
System.setOut(originalOut);
}
}
}