Skip to content

Closes #13275 : export footer only for commands that support --output #13277

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions jabkit/src/main/java/org/jabref/JabKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public static void main(String[] args) {
}
}

private static void applyUsageFooters(CommandLine commandLine,
List<Pair<String, String>> inputFormats,
List<Pair<String, String>> outputFormats,
Set<SearchBasedFetcher> fetchers) {
static void applyUsageFooters(CommandLine commandLine,
List<Pair<String, String>> inputFormats,
List<Pair<String, String>> outputFormats,
Set<SearchBasedFetcher> fetchers) {
String inputFooter = "\n"
+ Localization.lang("Available import formats:") + "\n"
+ StringUtil.alignStringTable(inputFormats);
Expand All @@ -106,7 +106,7 @@ private static void applyUsageFooters(CommandLine commandLine,
boolean hasInputOption = subCommand.getCommandSpec().options().stream()
.anyMatch(opt -> Arrays.asList(opt.names()).contains("--input-format"));
boolean hasOutputOption = subCommand.getCommandSpec().options().stream()
.anyMatch(opt -> Arrays.asList(opt.names()).contains("--output-format"));
.anyMatch(opt -> Arrays.asList(opt.names()).contains("--output"));

String footerText = "";
footerText += hasInputOption ? inputFooter : "";
Expand Down
88 changes: 88 additions & 0 deletions jabkit/src/test/java/org/jabref/HelpOutputTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.jabref;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

import javafx.collections.FXCollections;
import javafx.util.Pair;

import org.jabref.cli.ArgumentProcessor;
import org.jabref.logic.exporter.ExportPreferences;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.importer.ImporterPreferences;
import org.jabref.logic.importer.SearchBasedFetcher;
import org.jabref.logic.importer.WebFetchers;
import org.jabref.logic.preferences.CliPreferences;
import org.jabref.model.entry.BibEntryTypesManager;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;
import picocli.CommandLine;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class HelpOutputTest {

private CliPreferences preferences;
private BibEntryTypesManager entryTypesManager;
private CommandLine cmd;

@BeforeEach
public void setUp() {
preferences = mock(CliPreferences.class, Answers.RETURNS_DEEP_STUBS);
entryTypesManager = mock(BibEntryTypesManager.class);

ImporterPreferences importerPreferences = mock(ImporterPreferences.class, Answers.RETURNS_DEEP_STUBS);
ExportPreferences exportPreferences = mock(ExportPreferences.class, Answers.RETURNS_DEEP_STUBS);
ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);

when(preferences.getImporterPreferences()).thenReturn(importerPreferences);
when(preferences.getExportPreferences()).thenReturn(exportPreferences);
when(preferences.getImportFormatPreferences()).thenReturn(importFormatPreferences);

when(exportPreferences.getCustomExporters()).thenReturn(FXCollections.emptyObservableList());
Copy link

Choose a reason for hiding this comment

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

In JabRef, to create an empty list we use List.of() instead of FXCollections.emptyObservableList() to maintain consistency with the rest of the codebase.

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

Choose a reason for hiding this comment

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

In JabRef, to create an empty set we use Set.of() instead of FXCollections.emptyObservableSet() to maintain consistency with the rest of the codebase.


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

@Test
public void testExportFormatFooterShownOnlyForCommandsWithOutputOption() {
List<Pair<String, String>> inputFormats = ArgumentProcessor.getAvailableImportFormats(preferences);
List<Pair<String, String>> outputFormats = ArgumentProcessor.getAvailableExportFormats(preferences);
Set<SearchBasedFetcher> fetchers = WebFetchers.getSearchBasedFetchers(
preferences.getImportFormatPreferences(),
preferences.getImporterPreferences()
);

JabKit.applyUsageFooters(cmd, inputFormats, outputFormats, fetchers);

cmd.getSubcommands().forEach((name, subCmd) -> {
CommandLine.Model.CommandSpec spec = subCmd.getCommandSpec();
String[] footer = subCmd.getCommandSpec().usageMessage().footer();
String target = "Available export formats";

boolean hasOutputOption = spec.options().stream()
.anyMatch(opt -> Arrays.asList(opt.names()).contains("--output"));
boolean containsExportFormats = Arrays.stream(footer)
.anyMatch(line -> line.contains(target));

if ("fetch".equals(name)) {
// special case: expect footer NOT present
assertEquals(false, containsExportFormats,
Copy link

Choose a reason for hiding this comment

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

Assert the contents of objects (assertEquals), not checking for some Boolean conditions (assertTrue/assertFalse) to improve readability and maintainability.

"Did not expect 'Available export formats' in footer for special case: " + name);
} else if (hasOutputOption) {
assertEquals(true, containsExportFormats,
Copy link

Choose a reason for hiding this comment

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

Assert the contents of objects (assertEquals), not checking for some Boolean conditions (assertTrue/assertFalse) to improve readability and maintainability.

"Expected 'Available export formats' in footer for: " + name);
} else {
assertEquals(false, containsExportFormats,
"Did not expect 'Available export formats' in footer for: " + name);
}
});
}
}
Loading