Skip to content

Commit

Permalink
NCL-6053: Prevent .bacon directories being created during tests. Migr…
Browse files Browse the repository at this point in the history
…ate testing library
  • Loading branch information
rnc committed Mar 6, 2025
1 parent 985d06d commit ce371a1
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 208 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ __pycache__
**/.settings
.vscode

.bacon

.cache

cli/dependency-reduced-pom.xml
4 changes: 2 additions & 2 deletions cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
347 changes: 174 additions & 173 deletions cli/src/test/java/org/jboss/pnc/bacon/cli/AppTest.java

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
</dependency>

<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
package org.jboss.pnc.bacon.common;

import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.stream.SystemOut;
import ch.qos.logback.classic.Level;

@ExtendWith(SystemStubsExtension.class)
class ObjectHelperTest {

@SystemStub
private SystemOut systemOut;

@Test
void printJson() throws Exception {
String actual = tapSystemOut(() -> {
Map<String, String> testSubject = new HashMap<>();
Map<String, String> testSubject = new HashMap<>();

testSubject.put("test", "subject");
testSubject.put("test", "subject");

ObjectHelper.print(true, testSubject);
});
ObjectHelper.print(true, testSubject);

String expected = String.format("{\"test\":\"subject\"}%n");

assertEquals(expected, actual);
assertEquals(expected, systemOut.getText());
}

@Test
void printYaml() throws Exception {
String actual = tapSystemOut(() -> {
Map<String, String> testSubject = new HashMap<>();
Map<String, String> testSubject = new HashMap<>();

testSubject.put("test", "subject");
testSubject.put("test", "subject");

ObjectHelper.print(false, testSubject);
});
ObjectHelper.print(false, testSubject);

String expected = String.format("---%ntest: \"subject\"%n%n");

assertEquals(expected, actual);
assertEquals(expected, systemOut.getText());
}

@Test
Expand Down
5 changes: 5 additions & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.util.Base64;
import java.util.List;
import java.util.Random;

import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -83,8 +84,8 @@ protected <T> T executeAndDeserializeJSON(Class<T> clazz, String... args) throws
return result.fromYAML(clazz);
}

protected ExecutionResult executeAndGetResult(String... args) {
return CLIExecutor.runCommand(args);
protected ExecutionResult executeAndGetResult(List<String> env, String... args) {
return CLIExecutor.runCommand(env, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.jboss.pnc.bacon.common.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author jbrazdil
*/
public final class CLIExecutor {
private static final Logger log = LoggerFactory.getLogger(CLIExecutor.class);

private static final Path BACON_JAR = Paths.get("..", "cli", "target", "bacon.jar").toAbsolutePath().normalize();

Expand All @@ -34,6 +34,10 @@ private CLIExecutor() {
}

public static ExecutionResult runCommand(String... args) {
return runCommand(Collections.emptyList(), args);
}

public static ExecutionResult runCommand(List<String> optionalEnv, String... args) {
try {
checkExecutable();

Expand All @@ -42,12 +46,14 @@ public static ExecutionResult runCommand(String... args) {
cmdarray[1] = "-jar";
cmdarray[2] = BACON_JAR.toString();
System.arraycopy(args, 0, cmdarray, 3, args.length);
String[] env = { Constant.CONFIG_ENV + "=" + CONFIG_LOCATION };
List<String> env = new ArrayList<>();
env.add(Constant.CONFIG_ENV + "=" + CONFIG_LOCATION);
env.addAll(optionalEnv);

System.out.println(
"Running command: " + Arrays.stream(cmdarray).collect(Collectors.joining("' '", "'", "'"))
+ "\n\twith env " + Arrays.toString(env));
Process process = Runtime.getRuntime().exec(cmdarray, env);
+ "\n\twith env " + env);
Process process = Runtime.getRuntime().exec(cmdarray, env.toArray(String[]::new));

CompletableFuture<String> output = CompletableFuture
.supplyAsync(() -> readInputStream(process.getInputStream()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jboss.pnc.bacon.common.Constant;
import org.jboss.pnc.bacon.test.AbstractTest;
import org.jboss.pnc.bacon.test.ExecutionResult;
import org.jboss.pnc.bacon.test.TestType;
Expand All @@ -44,6 +45,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.io.TempDir;

/**
*
Expand All @@ -69,7 +71,7 @@ class PigTest extends AbstractTest {

@Test
@Order(1)
void shouldCreateProduct() throws IOException {
void shouldCreateProduct(@TempDir Path contextDir) throws Exception {
final Path configFile = CONFIG_LOCATION;
replaceSuffixInConfigFile(configFile.resolve("build-config.yaml"));

Expand Down Expand Up @@ -146,7 +148,11 @@ void shouldCreateProduct() throws IOException {
.post(GROUP_CONFIG_BUILD_CONFIGS.apply(UNIVERSAL_ID))
.then()
.getEntity(GROUP_CONFIG, groupConfigWithBuildConfig);
ExecutionResult output = executeAndGetResult("pig", "configure", configFile.toString());
ExecutionResult output = executeAndGetResult(
List.of(Constant.PIG_CONTEXT_DIR + "=" + contextDir.toString()),
"pig",
"configure",
configFile.toString());
assertThat(output.getOutput()).contains("name: \"Product Foobar " + SUFFIX + "\"");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ void shouldGetInvalidProduct() throws JsonProcessingException {

wmock.get(PRODUCT, productId);

ExecutionResult er = executeAndGetResult("pnc", "product", "get", productId);
ExecutionResult er = executeAndGetResult(Collections.emptyList(), "pnc", "product", "get", productId);

assertEquals(er.getRetval(), 1);
assertEquals(1, er.getRetval());
assertTrue(er.getError().contains("HTTP 404 Not Found"));
}
}
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>1.2.1</version>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>2.1.7</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit ce371a1

Please sign in to comment.