add environment arguments to commandline process factory
[teamcity/dotNetPackagesSupport.git] / nuget-tests / src / jetbrains / buildServer / nuget / tests / integration / IntegrationTestBase.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package jetbrains.buildServer.nuget.tests.integration;\r
18 \r
19 import com.intellij.execution.configurations.GeneralCommandLine;\r
20 import jetbrains.buildServer.ExecResult;\r
21 import jetbrains.buildServer.RunBuildException;\r
22 import jetbrains.buildServer.SimpleCommandLineProcessRunner;\r
23 import jetbrains.buildServer.agent.*;\r
24 import jetbrains.buildServer.nuget.agent.commands.NuGetActionFactory;\r
25 import jetbrains.buildServer.nuget.agent.commands.impl.CommandFactoryImpl;\r
26 import jetbrains.buildServer.nuget.agent.commands.impl.LoggingNuGetActionFactoryImpl;\r
27 import jetbrains.buildServer.nuget.agent.commands.impl.NuGetActionFactoryImpl;\r
28 import jetbrains.buildServer.nuget.agent.dependencies.NuGetPackagesCollector;\r
29 import jetbrains.buildServer.nuget.agent.dependencies.impl.NuGetPackagesCollectorImpl;\r
30 import jetbrains.buildServer.nuget.agent.dependencies.PackageUsages;\r
31 import jetbrains.buildServer.nuget.agent.dependencies.impl.NuGetPackagesConfigParser;\r
32 import jetbrains.buildServer.nuget.agent.dependencies.impl.PackageUsagesImpl;\r
33 import jetbrains.buildServer.nuget.agent.parameters.*;\r
34 import jetbrains.buildServer.nuget.agent.util.BuildProcessBase;\r
35 import jetbrains.buildServer.nuget.agent.util.CommandlineBuildProcessFactory;\r
36 import jetbrains.buildServer.nuget.tests.util.BuildProcessTestCase;\r
37 import org.jetbrains.annotations.NotNull;\r
38 import org.jmock.Expectations;\r
39 import org.jmock.Mockery;\r
40 import org.testng.annotations.BeforeMethod;\r
41 \r
42 import java.io.File;\r
43 import java.util.Collection;\r
44 import java.util.HashMap;\r
45 import java.util.Map;\r
46 \r
47 /**\r
48  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
49  * Date: 22.07.11 1:26\r
50  */\r
51 public class IntegrationTestBase extends BuildProcessTestCase {\r
52   private StringBuilder myCommandsOutput;\r
53   protected File myRoot;\r
54   protected Mockery m;\r
55   protected AgentRunningBuild myBuild;\r
56   protected BuildRunnerContext myContext;\r
57   protected BuildProgressLogger myLogger;\r
58   protected PackagesParametersFactory myParametersFactory;\r
59   protected NuGetFetchParameters myNuGet;\r
60   protected NuGetPackagesCollector myCollector;\r
61   protected NuGetActionFactory myActionFactory;\r
62   private BuildProcess myMockProcess;\r
63 \r
64   @NotNull\r
65   protected String getCommandsOutput() {\r
66     return myCommandsOutput.toString();\r
67   }\r
68 \r
69   @BeforeMethod\r
70   @Override\r
71   protected void setUp() throws Exception {\r
72     super.setUp();\r
73     myCommandsOutput = new StringBuilder();\r
74     myRoot = createTempDir();\r
75     m = new Mockery();\r
76     myBuild = m.mock(AgentRunningBuild.class);\r
77     myContext = m.mock(BuildRunnerContext.class);\r
78     myLogger = m.mock(BuildProgressLogger.class);\r
79     myParametersFactory = m.mock(PackagesParametersFactory.class);\r
80     myMockProcess = m.mock(BuildProcess.class);\r
81     myNuGet = m.mock(NuGetFetchParameters.class);\r
82 \r
83     m.checking(new Expectations() {{\r
84       allowing(myContext).getBuild();\r
85       will(returnValue(myBuild));\r
86       allowing(myBuild).getBuildLogger();\r
87       will(returnValue(myLogger));\r
88       allowing(myBuild).getCheckoutDirectory();\r
89       will(returnValue(myRoot));\r
90 \r
91       allowing(myMockProcess).start();\r
92       allowing(myMockProcess).waitFor();\r
93       will(returnValue(BuildFinishedStatus.FINISHED_SUCCESS));\r
94 \r
95       allowing(myLogger).message(with(any(String.class)));\r
96     }});\r
97 \r
98     myCollector = new NuGetPackagesCollectorImpl();\r
99     PackageUsages pu = new PackageUsagesImpl(\r
100             myCollector,\r
101             new NuGetPackagesConfigParser()\r
102     );\r
103 \r
104     myActionFactory = new LoggingNuGetActionFactoryImpl(new NuGetActionFactoryImpl(executingFactory(), pu, new CommandFactoryImpl()));\r
105   }\r
106 \r
107   @NotNull\r
108   protected File getTestDataPath(final String path) {\r
109     return Paths.getTestDataPath("integration/" + path);\r
110   }\r
111 \r
112   @NotNull\r
113   private CommandlineBuildProcessFactory executingFactory() {\r
114     return new CommandlineBuildProcessFactory() {\r
115       @NotNull\r
116       public BuildProcess executeCommandLine(@NotNull final BuildRunnerContext hostContext,\r
117                                              @NotNull final File program,\r
118                                              @NotNull final Collection<String> argz,\r
119                                              @NotNull final File workingDir,\r
120                                              @NotNull final Map<String, String> additionalEnvironment) throws RunBuildException {\r
121         return new BuildProcessBase() {\r
122           @NotNull\r
123           @Override\r
124           protected BuildFinishedStatus waitForImpl() throws RunBuildException {\r
125             GeneralCommandLine cmd = new GeneralCommandLine();\r
126             cmd.setExePath(program.getPath());\r
127             for (String arg : argz) {\r
128               cmd.addParameter(arg);\r
129             }\r
130             cmd.setWorkingDirectory(workingDir);\r
131 \r
132             Map<String, String> env = new HashMap<String, String>();\r
133             env.putAll(System.getenv());\r
134             env.putAll(additionalEnvironment);\r
135             cmd.setEnvParams(env);\r
136 \r
137             System.out.println("Run: " + cmd.getCommandLineString());\r
138 \r
139             ExecResult result = SimpleCommandLineProcessRunner.runCommand(cmd, new byte[0]);\r
140 \r
141             System.out.println(result.getStdout());\r
142             System.out.println(result.getStderr());\r
143 \r
144             myCommandsOutput.append(result.getStdout()).append("\n\n").append(result.getStderr()).append("\n\n");\r
145 \r
146             return result.getExitCode() == 0\r
147                     ? BuildFinishedStatus.FINISHED_SUCCESS\r
148                     : BuildFinishedStatus.FINISHED_FAILED;\r
149           }\r
150         };\r
151       }\r
152     };\r
153   }\r
154 }\r