Skip to content

Commit

Permalink
chore: support injectable env vars for testing (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
burkedavison authored Oct 13, 2023
1 parent ae8c6d6 commit 11dfa93
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 37 deletions.
6 changes: 0 additions & 6 deletions third_party/docfx-doclet-143274/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,6 @@
<version>4.23.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ public void init(Locale locale, Reporter reporter) {

@Override
public boolean run(DocletEnvironment environment) {
String artifactVersion = System.getenv("artifactVersion");
String librariesBomVersion = System.getenv("librariesBomVersion");
String repoMetadataFilePath = System.getenv("repoMetadataFilePath");
Objects.requireNonNull(
repoMetadataFilePath, "Environment variable 'repoMetadataFilePath' must not be null.");
reporter.print(Kind.NOTE, "Environment variable artifactVersion: " + artifactVersion);
reporter.print(Kind.NOTE, "Environment variable librariesBomVersion: " + librariesBomVersion);
reporter.print(Kind.NOTE, "Environment variable repoMetadataFilePath: " + repoMetadataFilePath);
Objects.requireNonNull(repoMetadataFilePath, "repoMetadataFilePath must not be null.");

reporter.print(Kind.NOTE, "artifactVersion: " + artifactVersion);
reporter.print(Kind.NOTE, "librariesBomVersion: " + librariesBomVersion);
reporter.print(Kind.NOTE, "repoMetadataFilePath: " + repoMetadataFilePath);
reporter.print(Kind.NOTE, "Output path: " + outputPath);
reporter.print(Kind.NOTE, "Excluded packages: " + Arrays.toString(excludePackages));
reporter.print(Kind.NOTE, "Excluded classes: " + Arrays.toString(excludeClasses));
Expand Down Expand Up @@ -61,6 +58,9 @@ public String getName() {
private String projectName;
private boolean disableChangelog;
private boolean disableLibraryOverview;
private String artifactVersion;
private String librariesBomVersion;
private String repoMetadataFilePath;

@Override
public Set<? extends Option> getSupportedOptions() {
Expand Down Expand Up @@ -130,6 +130,36 @@ public boolean process(String option, List<String> arguments) {
return true;
}
},
new CustomOption(
"Artifact Version",
Arrays.asList("-artifactVersion", "--artifactVersion"),
"artifactVersion") {
@Override
public boolean process(String option, List<String> arguments) {
artifactVersion = arguments.get(0);
return true;
}
},
new CustomOption(
"libraries-bom Version",
Arrays.asList("-librariesBomVersion", "--librariesBomVersion"),
"librariesBomVersion") {
@Override
public boolean process(String option, List<String> arguments) {
librariesBomVersion = arguments.get(0);
return true;
}
},
new CustomOption(
"repo-metadata.json File Path",
Arrays.asList("-repoMetadataFilePath", "--repoMetadataFilePath"),
"repoMetadataFilePath") {
@Override
public boolean process(String option, List<String> arguments) {
repoMetadataFilePath = arguments.get(0);
return true;
}
},

// Support next properties for compatibility with Gradle javadoc task.
// According to javadoc spec - these properties used by StandardDoclet and used only when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.microsoft.doclet;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.microsoft.util.OptionsFileUtil;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,7 +22,18 @@ public static void main(final String[] args) {
return;
}

List<String> combined = new ArrayList<>();
run(
args,
new EnvironmentToArgumentsBuilder()
.addIfExists("artifactVersion")
.addIfExists("librariesBomVersion")
.addIfExists("repoMetadataFilePath")
.build());
}

@VisibleForTesting
static void run(final String[] args, List<String> env) {
List<String> combined = new ArrayList<>(env);
for (String arg : args) {
if (!(new java.io.File(arg)).isFile()) {
System.err.println(String.format("File '%s' does not exist", args[0]));
Expand All @@ -30,4 +43,27 @@ public static void main(final String[] args) {
ToolProvider.getSystemDocumentationTool()
.run(null, null, null, combined.toArray(new String[0]));
}

@VisibleForTesting
static class EnvironmentToArgumentsBuilder {
private final ImmutableList.Builder<String> env = new ImmutableList.Builder<>();

public EnvironmentToArgumentsBuilder addIfExists(String name) {
String value = System.getenv(name);
if (value != null) {
return add(name, value);
}
return this;
}

@VisibleForTesting
EnvironmentToArgumentsBuilder add(String name, String value) {
env.add("-" + name, value);
return this;
}

public ImmutableList<String> build() {
return env.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import com.microsoft.util.FileUtilTest;
Expand All @@ -15,9 +14,7 @@
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

public class DocletRunnerTest {

Expand All @@ -30,8 +27,6 @@ public class DocletRunnerTest {
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;

@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

@Before
public void cleanup() throws IOException {
FileUtilTest.deleteDirectory(OUTPUT_DIR);
Expand Down Expand Up @@ -71,17 +66,14 @@ public void testFilesGenerationWhenTargetFileDoesNotExist() {

@Test
public void testFilesGeneration() throws IOException {
environmentVariables.set("artifactVersion", "0.18.0");
environmentVariables.set("librariesBomVersion", "26.19.0");
environmentVariables.set(
"repoMetadataFilePath", "./src/test/java/com/microsoft/samples/.repo-metadata.json");
assertEquals("0.18.0", System.getenv("artifactVersion"));
assertEquals("26.19.0", System.getenv("librariesBomVersion"));
assertEquals(
"./src/test/java/com/microsoft/samples/.repo-metadata.json",
System.getenv("repoMetadataFilePath"));

DocletRunner.main(new String[] {PARAMS_DIR});
DocletRunner.run(
new String[] {PARAMS_DIR},
new DocletRunner.EnvironmentToArgumentsBuilder()
.add("artifactVersion", "0.18.0")
.add("librariesBomVersion", "26.19.0")
.add(
"repoMetadataFilePath", "./src/test/java/com/microsoft/samples/.repo-metadata.json")
.build());

List<Path> expectedFilePaths =
Files.list(Path.of(EXPECTED_GENERATED_FILES_DIR)).sorted().collect(Collectors.toList());
Expand Down Expand Up @@ -111,12 +103,6 @@ public void testFilesGeneration() throws IOException {
generatedFileLines[i]);
}
}
environmentVariables.clear("artifactVersion");
environmentVariables.clear("librariesBomVersion");
environmentVariables.clear("repoMetadataFilePath");
assertNull(System.getenv("artifactVersion"));
assertNull(System.getenv("librariesBomVersion"));
assertNull(System.getenv("repoMetadataFilePath"));
}

public void assertSameFileNames(List<Path> expected, List<Path> generated) {
Expand Down

0 comments on commit 11dfa93

Please sign in to comment.