Skip to content

Commit

Permalink
#6 - Support Unix OS - add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay.Pianikov authored and Nikolay.Pianikov committed Nov 21, 2016
1 parent bdc2e4a commit 3604d3b
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jetbrains.buildServer.messages.serviceMessages.Message;
import org.jetbrains.annotations.NotNull;

public class RunAsCmdGenerator implements ResourceGenerator<RunAsCmdSettings> {
public class CmdGenerator implements ResourceGenerator<Params> {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String NORMAL_STATUS = "NORMAL";
private static final List<Replacement> OurReplacements = Collections.unmodifiableList(Arrays.asList(
Expand All @@ -21,7 +21,7 @@ public class RunAsCmdGenerator implements ResourceGenerator<RunAsCmdSettings> {

@NotNull
@Override
public String create(@NotNull final RunAsCmdSettings settings) {
public String create(@NotNull final Params settings) {
final StringBuilder sb = new StringBuilder();
sb.append("@ECHO OFF");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.jetbrains.annotations.NotNull;

public class RunAsCmdSettings {
public class Params {
private final String myCommandLine;

public RunAsCmdSettings(
public Params(
@NotNull final String commandLine) {
myCommandLine = commandLine;
}
Expand All @@ -20,7 +20,7 @@ public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

final RunAsCmdSettings that = (RunAsCmdSettings)o;
final Params that = (Params)o;

return getCommandLine().equals(that.getCommandLine());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RunAsPlatformSpecificSetupBuilder implements CommandLineSetupBuilde
private final ResourcePublisher myBeforeBuildPublisher;
private final ResourcePublisher myExecutableFilePublisher;
private final ResourceGenerator<Settings> mySettingsGenerator;
private final ResourceGenerator<RunAsCmdSettings> myRunAsCmdGenerator;
private final ResourceGenerator<Params> myRunAsCmdGenerator;
private final CommandLineArgumentsService myCommandLineArgumentsService;
private final FileAccessService myFileAccessService;
private final String myCommandFileExtension;
Expand All @@ -30,7 +30,7 @@ public RunAsPlatformSpecificSetupBuilder(
@NotNull final ResourcePublisher beforeBuildPublisher,
@NotNull final ResourcePublisher executableFilePublisher,
@NotNull final ResourceGenerator<Settings> settingsGenerator,
@NotNull final ResourceGenerator<RunAsCmdSettings> runAsCmdGenerator,
@NotNull final ResourceGenerator<Params> runAsCmdGenerator,
@NotNull final CommandLineArgumentsService commandLineArgumentsService,
@NotNull final FileAccessService fileAccessService,
@NotNull final String commandFileExtension) {
Expand Down Expand Up @@ -68,10 +68,10 @@ public Iterable<CommandLineSetup> build(@NotNull final CommandLineSetup commandL
cmdLineArgs.add(new CommandLineArgument(commandLineSetup.getToolPath(), CommandLineArgument.Type.PARAMETER));
cmdLineArgs.addAll(commandLineSetup.getArgs());

final RunAsCmdSettings runAsCmdSettings = new RunAsCmdSettings(myCommandLineArgumentsService.createCommandLineString(cmdLineArgs));
final Params params = new Params(myCommandLineArgumentsService.createCommandLineString(cmdLineArgs));

final File commandFile = myFileService.getTempFileName(myCommandFileExtension);
resources.add(new CommandLineFile(myExecutableFilePublisher, commandFile.getAbsoluteFile(), myRunAsCmdGenerator.create(runAsCmdSettings)));
resources.add(new CommandLineFile(myExecutableFilePublisher, commandFile.getAbsoluteFile(), myRunAsCmdGenerator.create(params)));

return Collections.singleton(
new CommandLineSetup(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jetbrains.buildServer.runAs.agent;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import jetbrains.buildServer.dotNet.buildRunner.agent.ResourceGenerator;
import jetbrains.buildServer.messages.serviceMessages.Message;
import org.jetbrains.annotations.NotNull;

public class ShGenerator implements ResourceGenerator<Params> {

@NotNull
@Override
public String create(@NotNull final Params settings) {
final StringBuilder sb = new StringBuilder();
sb.append(settings.getCommandLine());
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
<bean class="jetbrains.buildServer.runAs.agent.SettingsProviderImpl" />

<!-- Windows -->
<bean class="jetbrains.buildServer.runAs.agent.RunAsCmdGenerator" id="runAsCmdGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.CmdGenerator" id="cmdGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.WindowsSettingsGenerator" id="windowsSettingsGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.RunAsPlatformSpecificSetupBuilder" id="runAsWindowsSetupBuilder">
<constructor-arg ref="beforeBuildPublisher"/>
<constructor-arg ref="executableFilePublisher"/>
<constructor-arg ref="windowsSettingsGenerator"/>
<constructor-arg ref="runAsCmdGenerator"/>
<constructor-arg ref="cmdGenerator"/>
<constructor-arg type="java.lang.String" value=".cmd"/>
</bean>

<!-- Unix -->
<bean class="jetbrains.buildServer.runAs.agent.RunAsShGenerator" id="runAsShGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.ShGenerator" id="shGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.UnixSettingsGenerator" id="unixSettingsGenerator"/>
<bean class="jetbrains.buildServer.runAs.agent.RunAsPlatformSpecificSetupBuilder" id="runAsUnixSetupBuilder">
<constructor-arg ref="beforeBuildPublisher"/>
<constructor-arg ref="unixSettingsGenerator"/>
<constructor-arg ref="runAsShGenerator"/>
<constructor-arg ref="shGenerator"/>
<constructor-arg type="java.lang.String" value=".sh"/>
</bean>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package jetbrains.buildServer.runAs.agent;

import jetbrains.buildServer.dotNet.buildRunner.agent.ResourceGenerator;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.assertj.core.api.BDDAssertions.then;

public class RunAsCmdGeneratorTest {
public class CmdGeneratorTest {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");

@DataProvider(name = "cmdLinesCases")
Expand All @@ -31,10 +32,10 @@ public Object[][] getCmdLinesCases() {
@Test(dataProvider = "cmdLinesCases")
public void shouldGenerateContent(@NotNull final String cmdLine, @NotNull final String cmdLineInMessage) {
// Given
final RunAsCmdGenerator instance = createInstance();
final ResourceGenerator<Params> instance = createInstance();

// When
final String content = instance.create(new RunAsCmdSettings(cmdLine));
final String content = instance.create(new Params(cmdLine));

// Then
then(content).isEqualTo("@ECHO OFF"
Expand All @@ -45,8 +46,8 @@ public void shouldGenerateContent(@NotNull final String cmdLine, @NotNull final
}

@NotNull
private RunAsCmdGenerator createInstance()
private ResourceGenerator<Params> createInstance()
{
return new RunAsCmdGenerator();
return new CmdGenerator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,41 @@ public void setUp()
}

@Test()
public void shouldWriteFileToDiskWhenPublishBeforeBuild() throws IOException {
public void shouldInvokePublishBeforeBuildFileInBasePublisherAndMakeItExecutableForAllWhenPublishBeforeBuildFile() throws IOException {
// Given
final File file = new File("file");
final CommandLineExecutionContext executionContext = new CommandLineExecutionContext(0);
myCtx.checking(new Expectations() {{
oneOf(myResourcePublisher).publishBeforeBuildFile(executionContext, file, "content");

oneOf(myFileAccessService).makeExecutableForAll(file);
}});

final ExecutableFilePublisher instance = createInstance();

// When
// instance.publishBeforeBuildFile(new CommandLineExecutionContext(0), file, "content");
instance.publishBeforeBuildFile(executionContext, file, "content");

// Then
// myCtx.assertIsSatisfied();
myCtx.assertIsSatisfied();
}

@Test()
public void shouldInvokePublishAfterBuildArtifactFileInBasePublisherWhenPublishBeforeBuildFile() throws IOException {
// Given
final File file = new File("file");
final CommandLineExecutionContext executionContext = new CommandLineExecutionContext(0);
myCtx.checking(new Expectations() {{
oneOf(myResourcePublisher).publishAfterBuildArtifactFile(executionContext, file);
}});

final ExecutableFilePublisher instance = createInstance();

// When
instance.publishAfterBuildArtifactFile(executionContext, file);

// Then
myCtx.assertIsSatisfied();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RunAsPlatformSpecificSetupBuilderTest {
private ResourceGenerator<Settings> myCredentialsGenerator;
private CommandLineResource myCommandLineResource1;
private CommandLineResource myCommandLineResource2;
private ResourceGenerator<RunAsCmdSettings> myArgsGenerator;
private ResourceGenerator<Params> myArgsGenerator;
private CommandLineArgumentsService myCommandLineArgumentsService;
private SettingsProvider mySettingsProvider;
private ResourcePublisher myExecutableFilePublisher;
Expand All @@ -41,7 +41,7 @@ public void setUp()
//noinspection unchecked
myCredentialsGenerator = (ResourceGenerator<Settings>)myCtx.mock(ResourceGenerator.class, "WindowsSettingsGenerator");
//noinspection unchecked
myArgsGenerator = (ResourceGenerator<RunAsCmdSettings>)myCtx.mock(ResourceGenerator.class, "ArgsGenerator");
myArgsGenerator = (ResourceGenerator<Params>)myCtx.mock(ResourceGenerator.class, "ArgsGenerator");
//noinspection unchecked
myCommandLineResource1 = myCtx.mock(CommandLineResource.class, "Res1");
myCommandLineResource2 = myCtx.mock(CommandLineResource.class, "Res2");
Expand All @@ -64,7 +64,7 @@ public void shouldBuildSetup() throws IOException {
final String credentialsContent = "credentials content";
final String cmdContent = "args content";
final CommandLineSetup commandLineSetup = new CommandLineSetup(toolName, args, resources);
final RunAsCmdSettings runAsCmdSettings = new RunAsCmdSettings("cmd line");
final Params params = new Params("cmd line");
final List<CommandLineArgument> additionalArgs = Arrays.asList(new CommandLineArgument("arg1", CommandLineArgument.Type.PARAMETER), new CommandLineArgument("arg 2", CommandLineArgument.Type.PARAMETER));
final Settings settings = new Settings(user, password, additionalArgs);
myCtx.checking(new Expectations() {{
Expand All @@ -84,13 +84,14 @@ public void shouldBuildSetup() throws IOException {
oneOf(myCommandLineArgumentsService).createCommandLineString(with(any(List.class)));
will(returnValue("cmd line"));

oneOf(myArgsGenerator).create(runAsCmdSettings);
oneOf(myArgsGenerator).create(params);
will(returnValue(cmdContent));

oneOf(myRunnerParametersService).getToolPath(Constants.RUN_AS_TOOL_NAME);
will(returnValue(runAsToolPath));

oneOf(myFileService).validatePath(runAsTool);
oneOf(myFileAccessService).makeExecutableForMe(runAsTool);
}});

final CommandLineSetupBuilder instance = createInstance();
Expand All @@ -106,7 +107,7 @@ public void shouldBuildSetup() throws IOException {
myCommandLineResource1,
myCommandLineResource2,
new CommandLineFile(myBeforeBuildPublisher, credentialsFile.getAbsoluteFile(), credentialsContent),
new CommandLineFile(myBeforeBuildPublisher, cmdFile.getAbsoluteFile(), cmdContent));
new CommandLineFile(myExecutableFilePublisher, cmdFile.getAbsoluteFile(), cmdContent));

then(setup.getArgs()).containsExactly(
new CommandLineArgument(credentialsFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jetbrains.buildServer.runAs.agent;

import jetbrains.buildServer.dotNet.buildRunner.agent.ResourceGenerator;
import org.jetbrains.annotations.NotNull;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.assertj.core.api.BDDAssertions.then;

public class ShGeneratorTest {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");

@DataProvider(name = "cmdLinesCases")
public Object[][] getCmdLinesCases() {
return new Object[][] {
{ "cmd line", "cmd line" },
};
}

@Test(dataProvider = "cmdLinesCases")
public void shouldGenerateContent(@NotNull final String cmdLine, @NotNull final String cmdLineInMessage) {
// Given
final ResourceGenerator<Params> instance = createInstance();

// When
final String content = instance.create(new Params(cmdLine));

// Then
then(content).isEqualTo(cmdLine);
}

@NotNull
private ResourceGenerator<Params> createInstance()
{
return new ShGenerator();
}
}
Loading

0 comments on commit 3604d3b

Please sign in to comment.