re-implement hide of nuget api key in build logs, add more tests for it
[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.Collections;\r
45 import java.util.HashMap;\r
46 import java.util.Map;\r
47 \r
48 /**\r
49  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
50  * Date: 22.07.11 1:26\r
51  */\r
52 public class IntegrationTestBase extends BuildProcessTestCase {\r
53   private StringBuilder myCommandsOutput;\r
54   protected File myRoot;\r
55   protected Mockery m;\r
56   protected AgentRunningBuild myBuild;\r
57   protected BuildRunnerContext myContext;\r
58   protected BuildProgressLogger myLogger;\r
59   protected PackagesParametersFactory myParametersFactory;\r
60   protected NuGetFetchParameters myNuGet;\r
61   protected NuGetPackagesCollector myCollector;\r
62   protected NuGetActionFactory myActionFactory;\r
63   private BuildProcess myMockProcess;\r
64   protected BuildParametersMap myBuildParametersMap;\r
65   protected String cmd;\r
66 \r
67   @NotNull\r
68   protected String getCommandsOutput() {\r
69     return myCommandsOutput.toString();\r
70   }\r
71 \r
72   @BeforeMethod\r
73   @Override\r
74   protected void setUp() throws Exception {\r
75     super.setUp();\r
76     myCommandsOutput = new StringBuilder();\r
77     myRoot = createTempDir();\r
78     m = new Mockery();\r
79     myBuild = m.mock(AgentRunningBuild.class);\r
80     myContext = m.mock(BuildRunnerContext.class);\r
81     myLogger = m.mock(BuildProgressLogger.class);\r
82     myParametersFactory = m.mock(PackagesParametersFactory.class);\r
83     myMockProcess = m.mock(BuildProcess.class);\r
84     myNuGet = m.mock(NuGetFetchParameters.class);\r
85     myBuildParametersMap = m.mock(BuildParametersMap.class);\r
86 \r
87     cmd = System.getenv("ComSpec");\r
88 \r
89     m.checking(new Expectations(){{\r
90       allowing(myContext).getBuildParameters(); will(returnValue(myBuildParametersMap));\r
91       allowing(myBuildParametersMap).getEnvironmentVariables(); will(returnValue(Collections.singletonMap("ComSpec", cmd)));\r
92 \r
93       allowing(myContext).getBuild();\r
94       will(returnValue(myBuild));\r
95       allowing(myBuild).getBuildLogger();\r
96       will(returnValue(myLogger));\r
97       allowing(myBuild).getCheckoutDirectory();\r
98       will(returnValue(myRoot));\r
99 \r
100       allowing(myMockProcess).start();\r
101       allowing(myMockProcess).waitFor();\r
102       will(returnValue(BuildFinishedStatus.FINISHED_SUCCESS));\r
103 \r
104       allowing(myLogger).message(with(any(String.class)));\r
105     }});\r
106 \r
107     myCollector = new NuGetPackagesCollectorImpl();\r
108     PackageUsages pu = new PackageUsagesImpl(\r
109             myCollector,\r
110             new NuGetPackagesConfigParser()\r
111     );\r
112 \r
113     myActionFactory = new LoggingNuGetActionFactoryImpl(new NuGetActionFactoryImpl(executingFactory(), pu, new CommandFactoryImpl()));\r
114   }\r
115 \r
116   @NotNull\r
117   protected File getTestDataPath(final String path) {\r
118     return Paths.getTestDataPath("integration/" + path);\r
119   }\r
120 \r
121   @NotNull\r
122   private CommandlineBuildProcessFactory executingFactory() {\r
123     return new CommandlineBuildProcessFactory() {\r
124       @NotNull\r
125       public BuildProcess executeCommandLine(@NotNull final BuildRunnerContext hostContext,\r
126                                              @NotNull final String program,\r
127                                              @NotNull final Collection<String> argz,\r
128                                              @NotNull final File workingDir,\r
129                                              @NotNull final Map<String, String> additionalEnvironment) throws RunBuildException {\r
130         return new BuildProcessBase() {\r
131           @NotNull\r
132           @Override\r
133           protected BuildFinishedStatus waitForImpl() throws RunBuildException {\r
134             GeneralCommandLine cmd = new GeneralCommandLine();\r
135             cmd.setExePath(program);\r
136             for (String arg : argz) {\r
137               cmd.addParameter(arg.replaceAll("%+", "%"));\r
138             }\r
139             cmd.setWorkingDirectory(workingDir);\r
140 \r
141             Map<String, String> env = new HashMap<String, String>();\r
142             env.putAll(System.getenv());\r
143             env.putAll(additionalEnvironment);\r
144             cmd.setEnvParams(env);\r
145 \r
146             System.out.println("Run: " + cmd.getCommandLineString());\r
147 \r
148             ExecResult result = SimpleCommandLineProcessRunner.runCommand(cmd, new byte[0]);\r
149 \r
150             System.out.println(result.getStdout());\r
151             System.out.println(result.getStderr());\r
152 \r
153             myCommandsOutput.append(result.getStdout()).append("\n\n").append(result.getStderr()).append("\n\n");\r
154 \r
155             return result.getExitCode() == 0\r
156                     ? BuildFinishedStatus.FINISHED_SUCCESS\r
157                     : BuildFinishedStatus.FINISHED_FAILED;\r
158           }\r
159         };\r
160       }\r
161     };\r
162   }\r
163 }\r