Skip to content

Commit

Permalink
fix(#54): Version is configurable using version method (#76)
Browse files Browse the repository at this point in the history
Closes #54
  • Loading branch information
lordofthejars authored and bartoszmajsak committed Jul 28, 2017
1 parent 7b20191 commit 3d2325f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
Expand Up @@ -5,6 +5,7 @@ public class Configuration {
public static final String SMART_TESTING = "smart.testing";
public static final String SMART_TESTING_MODE = "smart.testing.mode";
public static final String SMART_TESTING_PLUGIN = "smart.testing.plugin";
public static final String SMART_TESTING_VERSION = "smart.testing.version";

private String[] strategies = new String[0];
private RunMode mode;
Expand Down
7 changes: 7 additions & 0 deletions functional-tests/test-bed/pom.xml
Expand Up @@ -29,6 +29,13 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<!-- Maven Extension JAR is added because we need to get version from extension_version file in case tester does not specify any version-->
<groupId>org.arquillian.smart.testing</groupId>
<artifactId>smart-testing-maven-lifecycle-extension</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.arquillian.smart.testing</groupId>
<artifactId>smart-testing-strategy-affected</artifactId>
Expand Down
Expand Up @@ -13,15 +13,13 @@ class MavenExtensionRegisterer {
private static final String EXTENSIONS_XML = "extensions.xml";
private static final String VERSION_PLACEHOLDER = "${version}";

private final ProjectConfigurator projectConfigurator;
private final Path rootPom;

MavenExtensionRegisterer(Path rootPom, ProjectConfigurator projectConfigurator) {
MavenExtensionRegisterer(Path rootPom) {
this.rootPom = rootPom;
this.projectConfigurator = projectConfigurator;
}

void addSmartTestingExtension() {
void addSmartTestingExtension(String version) {
final Path projectRoot = rootPom.getParent();
try {
final Path extensionFolder = Files.createDirectories(projectRoot.resolve(".mvn"));
Expand All @@ -30,7 +28,7 @@ void addSmartTestingExtension() {

try (BufferedReader br = new BufferedReader(new InputStreamReader(extensionFile))) {
final String extensionTemplate = br.lines().collect(Collectors.joining(System.lineSeparator()));
final String extensionContent = extensionTemplate.replace(VERSION_PLACEHOLDER, Project.SMART_TESTING_VERSION);
final String extensionContent = extensionTemplate.replace(VERSION_PLACEHOLDER, version);
Files.write(extensionFolder.resolve(EXTENSIONS_XML), extensionContent.getBytes());
}
} catch (IOException e) {
Expand Down
Expand Up @@ -9,8 +9,6 @@

public class Project implements AutoCloseable {

public static final String SMART_TESTING_VERSION = "0.0.1-SNAPSHOT"; // TODO make configurable

private final Path root;
private final Repository repository;
private final ProjectBuilder projectBuilder;
Expand Down
@@ -1,6 +1,8 @@
package org.arquillian.smart.testing.ftest.testbed.project;

import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
Expand All @@ -10,8 +12,13 @@

public class ProjectConfigurator {

private static final String SMART_TESTING = "smart.testing";
private static final String SMART_TESTING_MODE = "smart.testing.mode";
private static final String SMART_TESTING_VERSION = "smart.testing.version";

private Strategy[] strategies;
private Mode mode;
private String version;

private final Project project;
private final Path root;
Expand Down Expand Up @@ -39,13 +46,45 @@ public ProjectConfigurator inMode(Mode mode) {
return this;
}

public ProjectConfigurator version(String version) {
this.version = version;
return this;
}

public Project enable() {
final Path rootPom = Paths.get(root.toString(), File.separator, "pom.xml");
final MavenExtensionRegisterer mavenExtensionRegisterer = new MavenExtensionRegisterer(rootPom, this);
mavenExtensionRegisterer.addSmartTestingExtension();
final Path rootPom = Paths.get(root.toString(), "pom.xml");
final MavenExtensionRegisterer mavenExtensionRegisterer = new MavenExtensionRegisterer(rootPom);
String currentVersion = resolveVersion();
mavenExtensionRegisterer.addSmartTestingExtension(currentVersion);
final String strategies = Arrays.stream(getStrategies()).map(Strategy::getName).collect(Collectors.joining(","));
this.project.buildOptions().withSystemProperties("smart.testing", strategies, "smart.testing.mode", getMode().getName()).configure();
this.project.buildOptions()
.withSystemProperties(SMART_TESTING, strategies, SMART_TESTING_MODE, getMode().getName(),
SMART_TESTING_VERSION, currentVersion)
.configure();
return this.project;
}

private String resolveVersion() {

if (this.version == null) {
String systemProperty = System.getProperty(SMART_TESTING_VERSION);

if (systemProperty == null) {
return readVersionFromExtensionFile();
}

return systemProperty;
}

return this.version;
}

private String readVersionFromExtensionFile() {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(ProjectConfigurator.class.getResourceAsStream("/extension_version")))) {
return reader.readLine().trim();
} catch (IOException e) {
throw new RuntimeException("Couldn't read extension version", e);
}
}
}
Expand Up @@ -3,6 +3,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.arquillian.smart.testing.Configuration;

public class ExtensionVersion {

Expand All @@ -15,7 +16,7 @@ public static Version version() {

synchronized (ExtensionVersion.class) {
if (version == null) {
final String smartTestingVersion = System.getProperty("smart.testing.version", NO_VERSION);
final String smartTestingVersion = System.getProperty(Configuration.SMART_TESTING_VERSION, NO_VERSION);
if (NO_VERSION.equals(smartTestingVersion)) {
readFromFile();
} else {
Expand Down

0 comments on commit 3d2325f

Please sign in to comment.