Skip to content

Commit

Permalink
add possibility to specify report language via cli option (via #2476)
Browse files Browse the repository at this point in the history
  • Loading branch information
baev authored Apr 4, 2024
1 parent e99e39a commit 06f69d6
Show file tree
Hide file tree
Showing 40 changed files with 818 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,17 @@ public ExitCode run() {
generateCommand.isCleanReportDirectory(),
generateCommand.isSingleFileMode(),
generateCommand.getConfigOptions(),
generateCommand.getReportNameOptions()
generateCommand.getReportNameOptions(),
generateCommand.getReportLanguageOptions()
);
case SERVE_COMMAND:
return commands.serve(
serveCommand.getResultsOptions().getResultsDirectories(),
serveCommand.getHostPortOptions().getHost(),
serveCommand.getHostPortOptions().getPort(),
serveCommand.getConfigOptions(),
serveCommand.getReportNameOptions()
serveCommand.getReportNameOptions(),
serveCommand.getReportLanguageOptions()
);
case OPEN_COMMAND:
return commands.open(
Expand Down
146 changes: 122 additions & 24 deletions allure-commandline/src/main/java/io/qameta/allure/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.Plugin;
import io.qameta.allure.option.ConfigOptions;
import io.qameta.allure.option.ReportLanguageOptions;
import io.qameta.allure.option.ReportNameOptions;
import io.qameta.allure.plugin.DefaultPluginLoader;
import org.apache.commons.io.FileUtils;
Expand All @@ -45,6 +46,8 @@
import static java.lang.String.format;

/**
* The type Commands.
*
* @author charlie (Dmitry Baev).
*/
@SuppressWarnings({"ClassDataAbstractionCoupling", "ClassFanOutComplexity", "ReturnCount"})
Expand All @@ -56,17 +59,34 @@ public class Commands {

private final Path allureHome;

/**
* Instantiates a new Commands.
*
* @param allureHome the allure home
*/
public Commands(final Path allureHome) {
this.allureHome = allureHome;
}

/**
* Gets config.
*
* @param configOptions the config options
* @return the config
*/
public CommandlineConfig getConfig(final ConfigOptions configOptions) {
return getConfigFile(configOptions)
.map(ConfigLoader::new)
.map(ConfigLoader::load)
.orElseGet(CommandlineConfig::new);
}

/**
* Gets config file.
*
* @param configOptions the config options
* @return the config file
*/
public Optional<Path> getConfigFile(final ConfigOptions configOptions) {
if (Objects.nonNull(configOptions.getConfigPath())) {
return Optional.of(Paths.get(configOptions.getConfigPath()));
Expand All @@ -82,34 +102,41 @@ public Optional<Path> getConfigFile(final ConfigOptions configOptions) {
return Optional.empty();
}

/**
* Gets config file name.
*
* @param profile the profile
* @return the config file name
*/
public String getConfigFileName(final String profile) {
return Objects.isNull(profile)
? "allure.yml"
: format("allure-%s.yml", profile);
}

public ExitCode generate(final Path reportDirectory,
final List<Path> resultsDirectories,
final boolean clean,
final ConfigOptions profile,
final ReportNameOptions reportNameOptions) {
return generate(reportDirectory, resultsDirectories, clean, false, profile, reportNameOptions);
}

public ExitCode generate(final Path reportDirectory,
final List<Path> resultsDirectories,
final boolean clean,
final boolean singleFileMode,
final ConfigOptions profile,
final ReportNameOptions reportNameOptions) {
/**
* Generate exit code.
*
* @param reportDirectory the report directory
* @param resultsDirectories the results directories
* @param clean the clean
* @param singleFileMode the single file mode
* @param configuration the configuration
* @return the exit code
*/
private ExitCode generate(final Path reportDirectory,
final List<Path> resultsDirectories,
final boolean clean,
final boolean singleFileMode,
final Configuration configuration) {
final boolean directoryExists = Files.exists(reportDirectory);
if (clean && directoryExists) {
FileUtils.deleteQuietly(reportDirectory.toFile());
} else if (directoryExists && isDirectoryNotEmpty(reportDirectory)) {
LOGGER.error(DIRECTORY_EXISTS_MESSAGE, reportDirectory.toAbsolutePath());
return ExitCode.GENERIC_ERROR;
}
final ReportGenerator generator = new ReportGenerator(createReportConfiguration(profile, reportNameOptions));
final ReportGenerator generator = new ReportGenerator(configuration);
if (singleFileMode) {
generator.generateSingleFile(reportDirectory, resultsDirectories);
} else {
Expand All @@ -119,11 +146,48 @@ public ExitCode generate(final Path reportDirectory,
return ExitCode.NO_ERROR;
}

/**
* Generate exit code.
*
* @param reportDirectory the report directory
* @param resultsDirectories the results directories
* @param clean the clean
* @param singleFileMode the single file mode
* @param profile the profile
* @param reportNameOptions the report name options
* @return the exit code
*/
public ExitCode generate(final Path reportDirectory,
final List<Path> resultsDirectories,
final boolean clean,
final boolean singleFileMode,
final ConfigOptions profile,
final ReportNameOptions reportNameOptions,
final ReportLanguageOptions reportLanguageOptions) {
final Configuration configuration = createReportConfiguration(
profile, reportNameOptions, reportLanguageOptions
);

return generate(reportDirectory, resultsDirectories, clean, singleFileMode, configuration);
}

/**
* Serve exit code.
*
* @param resultsDirectories the results directories
* @param host the host
* @param port the port
* @param configOptions the config options
* @param reportNameOptions the report name options
* @param reportLanguageOptions the report language options
* @return the exit code
*/
public ExitCode serve(final List<Path> resultsDirectories,
final String host,
final int port,
final ConfigOptions configOptions,
final ReportNameOptions reportNameOptions) {
final ReportNameOptions reportNameOptions,
final ReportLanguageOptions reportLanguageOptions) {
LOGGER.info("Generating report to temp directory...");

final Path reportDirectory;
Expand All @@ -136,19 +200,31 @@ public ExitCode serve(final List<Path> resultsDirectories,
return ExitCode.GENERIC_ERROR;
}

final Configuration configuration = createReportConfiguration(
configOptions, reportNameOptions, reportLanguageOptions
);

final ExitCode exitCode = generate(
reportDirectory,
resultsDirectories,
false,
configOptions,
reportNameOptions
false,
configuration
);
if (exitCode.isSuccess()) {
return open(reportDirectory, host, port);
}
return exitCode;
}

/**
* Open exit code.
*
* @param reportDirectory the report directory
* @param host the host
* @param port the port
* @return the exit code
*/
public ExitCode open(final Path reportDirectory, final String host, final int port) {
LOGGER.info("Starting web server...");
final Server server;
Expand Down Expand Up @@ -179,6 +255,12 @@ public ExitCode open(final Path reportDirectory, final String host, final int po
return ExitCode.NO_ERROR;
}

/**
* List plugins exit code.
*
* @param configOptions the config options
* @return the exit code
*/
public ExitCode listPlugins(final ConfigOptions configOptions) {
final CommandlineConfig config = getConfig(configOptions);
config.getPlugins().forEach(System.out::println);
Expand All @@ -188,11 +270,15 @@ public ExitCode listPlugins(final ConfigOptions configOptions) {
/**
* Creates report configuration for a given profile.
*
* @param profile selected profile.
* @param profile selected profile.
* @param reportNameOptions the report name options
* @param reportLanguageOptions the report language options
* @return created report configuration.
*/
protected Configuration createReportConfiguration(final ConfigOptions profile,
final ReportNameOptions reportNameOptions) {
protected Configuration createReportConfiguration(
final ConfigOptions profile,
final ReportNameOptions reportNameOptions,
final ReportLanguageOptions reportLanguageOptions) {
final DefaultPluginLoader loader = new DefaultPluginLoader();
final CommandlineConfig commandlineConfig = getConfig(profile);
final ClassLoader classLoader = getClass().getClassLoader();
Expand All @@ -202,15 +288,22 @@ protected Configuration createReportConfiguration(final ConfigOptions profile,
.map(Optional::get)
.collect(Collectors.toList());

return new ConfigurationBuilder()
.useDefault()
.fromPlugins(plugins)
return ConfigurationBuilder
.bundled()
.withPlugins(plugins)
.withReportName(reportNameOptions.getReportName())
.withReportLanguage(reportLanguageOptions.getReportLanguage())
.build();
}

/**
* Set up Jetty server to serve Allure Report.
*
* @param host the host
* @param port the port
* @param reportDirectory the report directory
* @return self for method chaining
* @throws IOException the io exception
*/
protected Server setUpServer(final String host, final int port, final Path reportDirectory) throws IOException {
final Server server = Objects.isNull(host)
Expand All @@ -228,6 +321,9 @@ protected Server setUpServer(final String host, final int port, final Path repor

/**
* Open the given url in default system browser.
*
* @param url the url
* @throws IOException the io exception
*/
protected void openBrowser(final URI url) throws IOException {
if (Desktop.isDesktopSupported()) {
Expand All @@ -251,4 +347,6 @@ private boolean isDirectoryNotEmpty(final Path path) {
return false;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.beust.jcommander.ParametersDelegate;
import io.qameta.allure.convert.PathConverter;
import io.qameta.allure.option.ConfigOptions;
import io.qameta.allure.option.ReportLanguageOptions;
import io.qameta.allure.option.ReportNameOptions;
import io.qameta.allure.option.ResultsOptions;

Expand Down Expand Up @@ -63,6 +64,9 @@ public class GenerateCommand {
@ParametersDelegate
private ReportNameOptions reportNameOptions = new ReportNameOptions();

@ParametersDelegate
private ReportLanguageOptions reportLanguageOptions = new ReportLanguageOptions();

public boolean isCleanReportDirectory() {
return cleanReportDirectory;
}
Expand All @@ -83,6 +87,10 @@ public ReportNameOptions getReportNameOptions() {
return reportNameOptions;
}

public ReportLanguageOptions getReportLanguageOptions() {
return reportLanguageOptions;
}

public boolean isSingleFileMode() {
return singleFileMode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.beust.jcommander.ParametersDelegate;
import io.qameta.allure.option.ConfigOptions;
import io.qameta.allure.option.HostPortOptions;
import io.qameta.allure.option.ReportLanguageOptions;
import io.qameta.allure.option.ReportNameOptions;
import io.qameta.allure.option.ResultsOptions;

Expand All @@ -43,6 +44,9 @@ public class ServeCommand {
@ParametersDelegate
private ReportNameOptions reportNameOptions = new ReportNameOptions();

@ParametersDelegate
private ReportLanguageOptions reportLanguageOptions = new ReportLanguageOptions();

public ResultsOptions getResultsOptions() {
return resultsOptions;
}
Expand All @@ -58,4 +62,8 @@ public ConfigOptions getConfigOptions() {
public ReportNameOptions getReportNameOptions() {
return reportNameOptions;
}

public ReportLanguageOptions getReportLanguageOptions() {
return reportLanguageOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016-2024 Qameta Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.option;

import com.beust.jcommander.Parameter;
import io.qameta.allure.validator.LanguageValidator;

/**
* Contains profile options.
*
* @since 2.0
*/
@SuppressWarnings("PMD.ImmutableField")
public class ReportLanguageOptions {

@Parameter(
names = {"--lang", "--report-language"},
description = "The report language.",
validateWith = LanguageValidator.class
)
private String reportLanguage;

public String getReportLanguage() {
return reportLanguage;
}

}
Loading

0 comments on commit 06f69d6

Please sign in to comment.