Skip to content

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 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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) {
}
}

Copy link
Member

Choose a reason for hiding this comment

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

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
69 changes: 69 additions & 0 deletions jabkit/src/test/java/org/jabref/HelpOutputTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.jabref;

import javafx.collections.FXCollections;

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.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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
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());
when(importerPreferences.getCustomImporters()).thenReturn(FXCollections.emptyObservableSet());

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

JabKit.applyUsageFooters(
cmd,
ArgumentProcessor.getAvailableImportFormats(preferences),
ArgumentProcessor.getAvailableExportFormats(preferences),
WebFetchers.getSearchBasedFetchers(preferences.getImportFormatPreferences(), preferences.getImporterPreferences())
);
}

@Test
public void testExportFormatsFooterShownForOutputCommands() {
String help = cmd.getSubcommands().get("search").getUsageMessage();
assertTrue(help.contains("Available export formats"),
"'Available export formats' should appear in 'search' command help");
}

@Test
public void testExportFormatsFooterNotShownForCommandsWithoutOutput() {
String help = cmd.getSubcommands().get("check-consistency").getUsageMessage();
assertFalse(help.contains("Available export formats"),
"'Available export formats' should NOT appear in 'check-consistency' command help");
}
}
Loading