Skip to content
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

Add possibility to specify the report name #2229

Merged
merged 5 commits into from
Jan 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,17 @@ public ExitCode run() {
generateCommand.getResultsOptions().getResultsDirectories(),
generateCommand.isCleanReportDirectory(),
generateCommand.isSingleFileMode(),
generateCommand.getConfigOptions()
generateCommand.getConfigOptions(),
generateCommand.getReportNameOptions()
);
case SERVE_COMMAND:
return commands.serve(
serveCommand.getResultsOptions().getResultsDirectories(),
serveCommand.getHostPortOptions().getHost(),
serveCommand.getHostPortOptions().getPort(),
serveCommand.getConfigOptions());
serveCommand.getConfigOptions(),
serveCommand.getReportNameOptions()
);
case OPEN_COMMAND:
return commands.open(
openCommand.getReportDirectories().get(0),
Expand Down
21 changes: 14 additions & 7 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.ReportNameOptions;
import io.qameta.allure.plugin.DefaultPluginLoader;
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.server.Server;
Expand Down Expand Up @@ -90,23 +91,25 @@ public String getConfigFileName(final String profile) {
public ExitCode generate(final Path reportDirectory,
final List<Path> resultsDirectories,
final boolean clean,
final ConfigOptions profile) {
return generate(reportDirectory, resultsDirectories, clean, false, profile);
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 ConfigOptions profile,
final ReportNameOptions reportNameOptions) {
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));
final ReportGenerator generator = new ReportGenerator(createReportConfiguration(profile, reportNameOptions));
if (singleFileMode) {
generator.generateSingleFile(reportDirectory, resultsDirectories);
} else {
Expand All @@ -119,7 +122,8 @@ public ExitCode generate(final Path reportDirectory,
public ExitCode serve(final List<Path> resultsDirectories,
final String host,
final int port,
final ConfigOptions configOptions) {
final ConfigOptions configOptions,
final ReportNameOptions reportNameOptions) {
LOGGER.info("Generating report to temp directory...");

final Path reportDirectory;
Expand All @@ -136,7 +140,8 @@ public ExitCode serve(final List<Path> resultsDirectories,
reportDirectory,
resultsDirectories,
false,
configOptions
configOptions,
reportNameOptions
);
if (exitCode.isSuccess()) {
return open(reportDirectory, host, port);
Expand Down Expand Up @@ -186,7 +191,8 @@ public ExitCode listPlugins(final ConfigOptions configOptions) {
* @param profile selected profile.
* @return created report configuration.
*/
protected Configuration createReportConfiguration(final ConfigOptions profile) {
protected Configuration createReportConfiguration(final ConfigOptions profile,
final ReportNameOptions reportNameOptions) {
final DefaultPluginLoader loader = new DefaultPluginLoader();
final CommandlineConfig commandlineConfig = getConfig(profile);
final ClassLoader classLoader = getClass().getClassLoader();
Expand All @@ -199,6 +205,7 @@ protected Configuration createReportConfiguration(final ConfigOptions profile) {
return new ConfigurationBuilder()
.useDefault()
.fromPlugins(plugins)
.withReportName(reportNameOptions.getReportName())
.build();
}

Expand Down
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.ReportNameOptions;
import io.qameta.allure.option.ResultsOptions;

import java.nio.file.Path;
Expand Down Expand Up @@ -59,6 +60,9 @@ public class GenerateCommand {
@ParametersDelegate
private ConfigOptions configOptions = new ConfigOptions();

@ParametersDelegate
private ReportNameOptions reportNameOptions = new ReportNameOptions();

public boolean isCleanReportDirectory() {
return cleanReportDirectory;
}
Expand All @@ -75,6 +79,10 @@ public ConfigOptions getConfigOptions() {
return configOptions;
}

public ReportNameOptions getReportNameOptions() {
return reportNameOptions;
}

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.ReportNameOptions;
import io.qameta.allure.option.ResultsOptions;

/**
Expand All @@ -39,6 +40,9 @@ public class ServeCommand {
@ParametersDelegate
private ConfigOptions configOptions = new ConfigOptions();

@ParametersDelegate
private ReportNameOptions reportNameOptions = new ReportNameOptions();

public ResultsOptions getResultsOptions() {
return resultsOptions;
}
Expand All @@ -50,4 +54,8 @@ public HostPortOptions getHostPortOptions() {
public ConfigOptions getConfigOptions() {
return configOptions;
}

public ReportNameOptions getReportNameOptions() {
return reportNameOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2016-2023 Qameta Software OÜ
*
* 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;

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

@Parameter(
names = {"--name", "--report-name"},
description = "The report name."
)
private String reportName;

public String getReportName() {
return reportName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.qameta.allure;

import io.qameta.allure.option.ConfigOptions;
import io.qameta.allure.option.ReportNameOptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -115,8 +116,12 @@ void shouldRunGenerate(@TempDir final Path temp) throws IOException {
final Path secondResult = Files.createDirectories(temp.resolve("second"));
final List<Path> results = Arrays.asList(firstResult, secondResult);

when(commands.generate(eq(report), eq(results), eq(false), any(ConfigOptions.class)))
.thenReturn(NO_ERROR);
when(
commands.generate(
eq(report), eq(results), eq(false),
any(ConfigOptions.class), any(ReportNameOptions.class)
)
).thenReturn(NO_ERROR);

final Optional<ExitCode> exitCode = commandLine.parse(
GENERATE_COMMAND, firstResult.toString(), secondResult.toString(),
Expand All @@ -126,11 +131,53 @@ void shouldRunGenerate(@TempDir final Path temp) throws IOException {
.isEmpty();

final ExitCode code = commandLine.run();
verify(commands, times(1)).generate(eq(report), eq(results), eq(false), any(ConfigOptions.class));
verify(commands, times(1))
.generate(
eq(report), eq(results), eq(false),
any(ConfigOptions.class), any(ReportNameOptions.class)
);
assertThat(code)
.isEqualTo(NO_ERROR);
}

@Test
void shouldRunGenerateWithReportName(@TempDir final Path temp) throws IOException {
final Path report = Files.createDirectories(temp.resolve("report"));
final Path firstResult = Files.createDirectories(temp.resolve("first"));
final Path secondResult = Files.createDirectories(temp.resolve("second"));
final List<Path> results = Arrays.asList(firstResult, secondResult);
final String reportName = randomString();

when(
commands.generate(
eq(report), eq(results), eq(false),
any(ConfigOptions.class), any(ReportNameOptions.class)
)
).thenReturn(NO_ERROR);

final Optional<ExitCode> exitCode = commandLine.parse(
GENERATE_COMMAND, firstResult.toString(), secondResult.toString(),
"--output", report.toString(),
"--name", reportName
);
assertThat(exitCode)
.isEmpty();

final ArgumentCaptor<ReportNameOptions> captor = ArgumentCaptor.forClass(ReportNameOptions.class);
final ExitCode code = commandLine.run();
verify(commands, times(1))
.generate(
eq(report), eq(results), eq(false),
any(ConfigOptions.class), captor.capture()
);
assertThat(code)
.isEqualTo(NO_ERROR);

assertThat(captor.getValue())
.extracting(ReportNameOptions::getReportName)
.isEqualTo(reportName);
}

@Test
void shouldRunOpen(@TempDir final Path report) {
final int port = randomPort();
Expand Down Expand Up @@ -208,7 +255,11 @@ void shouldParseServeCommand(@TempDir final Path temp) throws IOException {

final ArgumentCaptor<ConfigOptions> captor = ArgumentCaptor.forClass(ConfigOptions.class);

when(commands.serve(eq(Arrays.asList(first, second)), eq(host), eq(port), captor.capture())).thenReturn(NO_ERROR);
when(commands.serve(
eq(Arrays.asList(first, second)), eq(host), eq(port),
captor.capture(), any(ReportNameOptions.class))
)
.thenReturn(NO_ERROR);
final ExitCode run = commandLine.run();
assertThat(run)
.isEqualTo(NO_ERROR);
Expand All @@ -219,6 +270,50 @@ void shouldParseServeCommand(@TempDir final Path temp) throws IOException {
.containsExactly(profile);
}

@Test
void shouldParseServeCommandWithReportName(@TempDir final Path temp) throws IOException {
final int port = randomPort();
final String host = randomString();
final String profile = randomString();
final String reportName = randomString();
final Path first = Files.createDirectories(temp.resolve("first"));
final Path second = Files.createDirectories(temp.resolve("second"));
final Optional<ExitCode> code = commandLine.parse(
SERVE_COMMAND,
"--port", String.valueOf(port),
"--host", host,
"--profile", profile,
"--report-name", reportName,
first.toString(), second.toString()
);

assertThat(code)
.isEmpty();

final ArgumentCaptor<ConfigOptions> captorConfig
= ArgumentCaptor.forClass(ConfigOptions.class);
final ArgumentCaptor<ReportNameOptions> captorReportName
= ArgumentCaptor.forClass(ReportNameOptions.class);

when(commands.serve(
eq(Arrays.asList(first, second)), eq(host), eq(port),
captorConfig.capture(), captorReportName.capture())
)
.thenReturn(NO_ERROR);
final ExitCode run = commandLine.run();
assertThat(run)
.isEqualTo(NO_ERROR);

assertThat(captorConfig.getAllValues())
.hasSize(1)
.extracting(ConfigOptions::getProfile)
.containsExactly(profile);

assertThat(captorReportName.getValue())
.extracting(ReportNameOptions::getReportName)
.isEqualTo(reportName);
}

@Test
void shouldValidatePortValue() {
final Optional<ExitCode> exitCode = commandLine.parse(SERVE_COMMAND, "--port", "213123");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.qameta.allure;

import io.qameta.allure.option.ConfigOptions;
import io.qameta.allure.option.ReportNameOptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

Expand Down Expand Up @@ -52,8 +53,10 @@ void shouldFailIfDirectoryExists(@TempDir final Path temp) throws Exception {
final Path reportPath = Files.createDirectories(temp.resolve("report"));
Files.createTempFile(reportPath, "some", ".txt");
final Commands commands = new Commands(home);
final ExitCode exitCode = commands.generate(reportPath, null, false,
null);
final ExitCode exitCode = commands.generate(
reportPath, Collections.emptyList(), false,
new ConfigOptions(), new ReportNameOptions()
);

assertThat(exitCode)
.isEqualTo(ExitCode.GENERIC_ERROR);
Expand Down Expand Up @@ -99,7 +102,10 @@ void shouldAllowEmptyReportDirectory(@TempDir final Path temp) throws Exception
when(options.getProfile()).thenReturn("test");
final Path reportPath = Files.createDirectories(temp.resolve("report"));
final Commands commands = new Commands(home);
final ExitCode exitCode = commands.generate(reportPath, Collections.emptyList(), false, options);
final ExitCode exitCode = commands.generate(
reportPath,
Collections.emptyList(), false, options, new ReportNameOptions()
);

assertThat(exitCode)
.isEqualTo(ExitCode.NO_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class ConfigurationBuilder {

private final List<Plugin> plugins = new ArrayList<>();

private String reportName;

public ConfigurationBuilder useDefault() {
final String allureVersion = getVersionFromFile()
.orElse(getVersionFromManifest().orElse("Undefined"));
Expand Down Expand Up @@ -138,8 +140,14 @@ public ConfigurationBuilder fromPlugins(final List<Plugin> plugins) {
return this;
}

public ConfigurationBuilder withReportName(final String reportName) {
this.reportName = reportName;
return this;
}

public Configuration build() {
return new DefaultConfiguration(
this.reportName,
Collections.unmodifiableList(extensions),
Collections.unmodifiableList(plugins)
);
Expand Down
Loading
Loading