Skip to content

Commit

Permalink
feat: integration tests with testcontainers
Browse files Browse the repository at this point in the history
fix: #86
  • Loading branch information
nqminhuit authored and Your Name committed Jul 1, 2024
1 parent c69e179 commit 9c45af0
Show file tree
Hide file tree
Showing 11 changed files with 677 additions and 47 deletions.
5 changes: 4 additions & 1 deletion Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ WORKDIR /app/gis
RUN mvn -q verify clean --fail-never
COPY . /app/gis
RUN apk add --no-cache git
RUN mvn -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean package

# temporal skip tests because IntTests need containers to run,
# and I don't know how to config them
RUN mvn -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn clean package -DskipTests

FROM quay.io/quarkus/ubi-quarkus-mandrel-builder-image:23.1-jdk-21
USER root
Expand Down
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<junit.version>5.10.2</junit.version>
<assertj.version>3.26.0</assertj.version>
<mockito.version>5.12.0</mockito.version>
<testcontainers.version>1.19.8</testcontainers.version>
<slf4j-test.version>2.0.13</slf4j-test.version>
<maven-surefire.version>3.3.0</maven-surefire.version>
<compiler.plugin.version>3.12.1</compiler.plugin.version>
<jar.plugin.version>3.3.0</jar.plugin.version>
Expand All @@ -28,6 +30,8 @@
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand All @@ -52,6 +56,24 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-test.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j-test.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/nqm/Gis.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ public static void main(String... args) {
var gis = new CommandLine(new Gis());
gis.setExecutionExceptionHandler(GLOBAL_EXCEPTION_HANLER);

int exitCode;
if (args.length == 0) {
exitCode = gis.execute(GIT_STATUS, "--one-line");
} else {
exitCode = gis.execute(args);
}
System.exit(exitCode);
gis.execute(args.length == 0
? new String[] {GIT_STATUS, "--one-line"}
: args);
}

}
12 changes: 5 additions & 7 deletions src/main/java/org/nqm/command/GitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.nqm.config.GisConfig;
import org.nqm.config.GisLog;
Expand Down Expand Up @@ -180,13 +178,13 @@ void listBranches(

@Command(name = "init", description = "init .gis-modules for current directory")
void init() throws IOException {
try (var stream = Files.list(Path.of("."))) {
var data = stream.filter(Files::isDirectory)
var currentDir = Path.of(currentDir());
try (var stream = Files.list(currentDir)) {
var lines = stream.filter(Files::isDirectory)
.map(Path::getFileName)
.map("path = %s"::formatted)
.collect(Collectors.joining("\n"))
.getBytes();
Files.write(Paths.get(".gis-modules"), data);
.toList();
Files.write(currentDir.resolve(".gis-modules"), lines);
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/nqm/utils/StdOutUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public class StdOutUtils {

private StdOutUtils() {}

public static final String CL_RESET = "\u001B[0m";
public static final String CL_BLACK = "\u001B[30m";
public static final String CL_RED = "\u001B[31m";
public static final String CL_GREEN = "\u001B[32m";
public static final String CL_RESET = "\u001B[0m";
public static final String CL_BLACK = "\u001B[30m";
public static final String CL_RED = "\u001B[31m";
public static final String CL_GREEN = "\u001B[32m";
public static final String CL_YELLOW = "\u001B[33m";
public static final String CL_BLUE = "\u001B[34m";
public static final String CL_BLUE = "\u001B[34m";
public static final String CL_PURPLE = "\u001B[35m";
public static final String CL_CYAN = "\u001B[36m";
public static final String CL_WHITE = "\u001B[37m";
public static final String CL_CYAN = "\u001B[36m";
public static final String CL_WHITE = "\u001B[37m";

private static final String UNTRACKED_SYM = "?";
private static final String RENAME_SYM = "2";
Expand Down
101 changes: 101 additions & 0 deletions src/test/java/org/nqm/GisIntTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.nqm;

import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.jupiter.api.Test;
import org.nqm.helper.GisConfigMock;
import org.nqm.helper.GitBaseTest;

class GisIntTest extends GitBaseTest {

// private static final Logger log = LoggerFactory.getLogger(GisIntTest.class);

@Override
protected void additionalSetup() {
GisConfigMock.mockCurrentDirectory("" + tempPath);
}

@Override
protected void additionalTeardown() {
GisConfigMock.close();
}

@Test
void gis_init_OK() throws IOException {
// given:
create_clone_gitRepositories("rem7_y", "rem8_c", "rem9_m");

// when:
Gis.main("init");

// then:
var markerFile = tempPath.resolve(".gis-modules");
assertThat(Files.exists(markerFile)).isTrue();
assertThat(Files.readAllLines(markerFile)).containsExactlyInAnyOrder(
"path = rem9_m",
"path = rem8_c",
"path = rem7_y");
}

@Test
void gis_setVerbose_OK() throws IOException {
// given:
create_clone_gitRepositories("rem1_i", "rem2_j", "rem3_k");
Gis.main("init");
ignoreMarkerFile();
git(tempPath, "init");

// when:
Gis.setVerbose(true);
Gis.main();

// then:
var sPath = "" + tempPath;
assertThat(stripColors.apply(outCaptor.toString()))
.containsExactlyInAnyOrder(
" [DEBUG] executing command '/usr/bin/git status -sb --ignore-submodules --porcelain=v2' under module '%s/rem1_i'"
.formatted(sPath),
" [DEBUG] executing command '/usr/bin/git status -sb --ignore-submodules --porcelain=v2' under module '%s/rem2_j'"
.formatted(sPath),
" [DEBUG] executing command '/usr/bin/git status -sb --ignore-submodules --porcelain=v2' under module '%s/rem3_k'"
.formatted(sPath),
" [DEBUG] executing command '/usr/bin/git status -sb --ignore-submodules --porcelain=v2' under module '%s'"
.formatted(sPath),
"rem1_i master",
"rem2_j master",
"rem3_k master",
"%s master .gitignore rem1_i rem2_j rem3_k".formatted("" + tempPath.subpath(1, tempPath.getNameCount())));
}

@Test
void gis_withoutMarkerFile_handleException() {
// given:
create_clone_gitRepositories("rem4_a");
git(tempPath, "init");
Gis.setVerbose(true);

// when:
Gis.main("status");

// then:
assertThat(stripColorsToString.apply(errCaptor.toString())).contains(
"org.nqm.exception.GisException: Could not find '.gis-modules' or '.gitmodules' under this directory!");
}

@Test
void gis_handleFolderNotInSubmodules_OK() throws IOException {
// given:
create_clone_gitRepositories("rem1_i", "rem2_j", "rem3_k");
Gis.main("init");
git(tempPath, "init");
Files.write(tempPath.resolve(".gis-modules"), "path = asdf".getBytes());

// when:
Gis.main();

// then:
assertThat(stripColors.apply(errCaptor.toString())).contains(
" ERROR: directory '%s/asdf' does not exist, will be ignored!".formatted("" + tempPath));
}
}
Loading

0 comments on commit 9c45af0

Please sign in to comment.