Skip to content

Commit

Permalink
fix(#104): creates unique folder for each test project in Test Bed
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak committed Aug 9, 2017
1 parent eb6218b commit 1604f26
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 46 deletions.
10 changes: 5 additions & 5 deletions docs/developing.adoc
Expand Up @@ -107,7 +107,7 @@ for example:
You can execute embedded build in Test Bed in the debug mode.
This gives better analysis of the build execution, as it lets us connect to the build execution to analyze behaviour of our extension in the runtime by debugging both Maven execution as well as Surefire Provider.

===== Features
===== Built-in DSL features

You can use project configuration DSL for enabling debug mode directly in the test code:

Expand All @@ -134,15 +134,15 @@ Following system properties are available:
If both system property and the programmatic option is used system property takes precedence.
====

===== Project Persistent
Project Persistence feature is included in Test Bed to store repository copy used by each and every test method to
facilitate analysis in case of test failure.
===== Storing project under test
Project Persistence feature is included in Test Bed to store repository used test's embedded build. This lets you execute
the same build outside of test execution for further analysis in case of failure.

By default this feature is enabled to copy repository only in case of test failure.

In order to copy repository for all tests irrespective of test result, explicitly set system property `test.bed.project.persist` to `true`.

Persisted project are located in `target/projects` with name followed by naming convention:
Persisted project are located in `target/test-bed-executions/[current timestamp]/` with name followed by naming convention:

`getClass().getSimpleName() + "_" + name.getMethodName()`

Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
Expand Down Expand Up @@ -50,10 +51,10 @@ private void failed(Throwable e, Description description) {
}

private void copyTmpProjectInTarget() {
String path = "target" + File.separator + "projects";
String path = "target" + File.separator + "test-bed-executions" + File.separator + Instant.now().toEpochMilli();
final File projectDir = new File(path);
if (!projectDir.exists()) {
projectDir.mkdir();
projectDir.mkdirs();
}
if (targetRepoPerTestFolder != null) {
final Path source = Paths.get(targetRepoPerTestFolder);
Expand Down
@@ -1,61 +1,49 @@
package org.arquillian.smart.testing.ftest.testbed;

import java.io.File;
import net.jcip.annotations.NotThreadSafe;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import org.arquillian.smart.testing.ftest.testbed.rules.GitClone;
import org.arquillian.smart.testing.ftest.testbed.rules.TestBed;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

import static org.assertj.core.api.Assertions.assertThat;

@NotThreadSafe
public class ProjectPersistTest {

@Rule
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

@Test
public void should_copy_failing_test_directory_in_target_if_test_is_failing() {
final Result result = JUnitCore.runClasses(ProjectPersistFail.class);

assertThat(result.wasSuccessful()).isFalse();
assertThat(new File("target/projects/smart-testing-dogfood-repo_ProjectPersistFail_should_fail")).isDirectory().exists();
}

@Test
public void temp_projects_should_copied_in_target_if_test_is_failing() {
public void should_store_project_under_test_to_two_separated_directories_when_the_same_test_fails_twice_in_a_row() throws IOException {
final Result firstRun = JUnitCore.runClasses(ProjectPersistFail.class);
final Result secondRun = JUnitCore.runClasses(ProjectPersistFail.class);

assertThat(firstRun.wasSuccessful()).isFalse();
assertThat(secondRun.wasSuccessful()).isFalse();
// it's rather shallow check, but if it's not equal it means second execution of the test failed for different reason
// it's rather shallow check, but if it's not equal it means second execution of the test failed for different reasons
assertThat(secondRun.getFailures()).hasSameSizeAs(firstRun.getFailures());
assertThat(new File("target/projects/smart-testing-dogfood-repo_ProjectPersistFail_should_fail")).isDirectory().exists();

assertThat(findPersistedProjects("smart-testing-dogfood-repo_ProjectPersistFail_should_fail")).hasSize(2);
}

@Test
public void temp_test_projects_should_not_copied_in_target_if_test_is_passing() {
public void should_not_store_project_under_test_directory_when_test_is_passing() throws IOException {
final Result result = JUnitCore.runClasses(ProjectPersistPass.class);

assertThat(result.wasSuccessful()).isTrue();
assertThat(new File("target/projects/smart-testing-dogfood-repo_ProjectPersistPass_should_pass")).doesNotExist();
assertThat(findPersistedProjects("smart-testing-dogfood-repo_ProjectPersistPass_should_pass")).isEmpty();
}

@Test
public void temp_test_projects_should_copied_in_target_if_test_is_passing_and_system_property_is_set() {
System.setProperty("test.bed.project.persist", "true");

final Result result = JUnitCore.runClasses(ProjectPersistPass1.class);

assertThat(result.wasSuccessful()).isTrue();
assertThat(new File("target/projects/smart-testing-dogfood-repo_ProjectPersistPass1_should_pass")).isDirectory();
private List<Path> findPersistedProjects(String projectName) throws IOException {
return Files.walk(new File("target" + File.separator + "test-bed-executions").toPath(), 2)
.filter(dir -> dir.toFile().getAbsolutePath().endsWith(projectName))
.collect(Collectors.toList());
}

public static class ProjectPersistFail {
Expand Down Expand Up @@ -85,16 +73,4 @@ public void should_pass() throws Exception {
}
}

public static class ProjectPersistPass1 {
@ClassRule
public static final GitClone GIT_CLONE = new GitClone();

@Rule
public TestBed testBed = new TestBed(GIT_CLONE);

@Test
public void should_pass() throws Exception {
Assert.assertTrue(true);
}
}
}
@@ -0,0 +1,56 @@
package org.arquillian.smart.testing.ftest.testbed;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import net.jcip.annotations.NotThreadSafe;
import org.arquillian.smart.testing.ftest.testbed.rules.GitClone;
import org.arquillian.smart.testing.ftest.testbed.rules.TestBed;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

import static org.assertj.core.api.Assertions.assertThat;

@NotThreadSafe
public class ProjectPersistUsingPropertyTest {

@Rule
public final RestoreSystemProperties restoreSystemProperties = new RestoreSystemProperties();

@Test
public void should_store_project_under_test_directory_when_test_is_passing_but_property_is_set() throws IOException {
System.setProperty("test.bed.project.persist", "true");

final Result result = JUnitCore.runClasses(ProjectPersistAnotherPass.class);

assertThat(result.wasSuccessful()).isTrue();
assertThat(findPersistedProjects("smart-testing-dogfood-repo_ProjectPersistAnotherPass_should_pass")).hasSize(1);
}

private List<Path> findPersistedProjects(String projectName) throws IOException {
return Files.walk(new File("target" + File.separator + "test-bed-executions").toPath(), 2)
.filter(dir -> dir.toFile().getAbsolutePath().endsWith(projectName))
.collect(Collectors.toList());
}

public static class ProjectPersistAnotherPass {
@ClassRule
public static final GitClone GIT_CLONE = new GitClone();

@Rule
public TestBed testBed = new TestBed(GIT_CLONE);

@Test
public void should_pass() throws Exception {
Assert.assertTrue(true);
}
}
}

0 comments on commit 1604f26

Please sign in to comment.