-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Enhance consistency check tests #13788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
356a2bc
52217d7
6b40edb
2712f4b
975fea4
cffee73
d9cbc95
f28d7b5
8bd4ff0
52c6ba4
4aa43f7
8e2dae4
0a8a87d
1f544d9
c30fa2b
6dd1b83
b00baec
da813ef
fe8b256
6ff559e
ef4209f
1a20187
1897db2
bd58b14
14da9c1
1ac04cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
|
@@ -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); | ||
Jenish-1235 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| @BeforeEach() | ||
| void setup() { | ||
| when(importerPreferences.getCustomImporters()).thenReturn(FXCollections.emptyObservableSet()); | ||
|
|
@@ -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"); | ||
| } | ||
Jenish-1235 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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"); | ||
|
||
| } | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -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")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also remove |
||
|
|
||
| BibtexImporter bibtexImporter = new BibtexImporter(importFormatPreferences, new DummyFileUpdateMonitor()); | ||
| List<BibEntry> expectedEntries = bibtexImporter.importDatabase(expectedBib).getDatabase().getEntries(); | ||
|
|
@@ -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); | ||
| } | ||
|
|
||
|
|
@@ -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()); | ||
|
|
@@ -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 | ||
|
|
@@ -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); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.