minor
[teamcity/dotNetPackagesSupport.git] / nuget-tests / src / jetbrains / buildServer / nuget / tests / integration / InstallPackageIntegtatoinTest.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.install.PackageUsages;\r
25 import jetbrains.buildServer.nuget.agent.install.PackagesInstallerRunner;\r
26 import jetbrains.buildServer.nuget.agent.install.impl.NuGetActionFactoryImpl;\r
27 import jetbrains.buildServer.nuget.agent.install.impl.NuGetPackagesCollectorImpl;\r
28 import jetbrains.buildServer.nuget.agent.install.impl.NuGetPackagesConfigParser;\r
29 import jetbrains.buildServer.nuget.agent.install.impl.PackageUsagesImpl;\r
30 import jetbrains.buildServer.nuget.agent.parameters.NuGetFetchParameters;\r
31 import jetbrains.buildServer.nuget.agent.parameters.PackagesInstallParameters;\r
32 import jetbrains.buildServer.nuget.agent.parameters.PackagesParametersFactory;\r
33 import jetbrains.buildServer.nuget.agent.parameters.PackagesUpdateParameters;\r
34 import jetbrains.buildServer.nuget.agent.util.BuildProcessBase;\r
35 import jetbrains.buildServer.nuget.agent.util.CommandlineBuildProcessFactory;\r
36 import jetbrains.buildServer.nuget.common.PackageInfo;\r
37 import jetbrains.buildServer.nuget.common.PackagesUpdateMode;\r
38 import jetbrains.buildServer.nuget.tests.util.BuildProcessTestCase;\r
39 import jetbrains.buildServer.util.ArchiveUtil;\r
40 import org.jetbrains.annotations.NotNull;\r
41 import org.jetbrains.annotations.Nullable;\r
42 import org.jmock.Expectations;\r
43 import org.jmock.Mockery;\r
44 import org.testng.Assert;\r
45 import org.testng.annotations.BeforeMethod;\r
46 import org.testng.annotations.Test;\r
47 \r
48 import java.io.File;\r
49 import java.util.*;\r
50 \r
51 /**\r
52  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
53  * Date: 08.07.11 2:15\r
54  */\r
55 public class InstallPackageIntegtatoinTest extends BuildProcessTestCase {\r
56   private File myRoot;\r
57   private Mockery m;\r
58   private AgentRunningBuild myBuild;\r
59   private BuildRunnerContext myContext;\r
60   private BuildProgressLogger myLogger;\r
61   private PackagesParametersFactory myParametersFactory;\r
62   private PackagesInstallParameters myInstall;\r
63   private PackagesUpdateParameters myUpdate;\r
64   private NuGetFetchParameters myNuGet;\r
65   private BuildProcess myMockProcess;\r
66 \r
67   @BeforeMethod\r
68   @Override\r
69   protected void setUp() throws Exception {\r
70     super.setUp();\r
71     myRoot = createTempDir();\r
72     m = new Mockery();\r
73     myBuild = m.mock(AgentRunningBuild.class);\r
74     myContext = m.mock(BuildRunnerContext.class);\r
75     myLogger = m.mock(BuildProgressLogger.class);\r
76     myParametersFactory = m.mock(PackagesParametersFactory.class);\r
77     myInstall = m.mock(PackagesInstallParameters.class);\r
78     myUpdate = m.mock(PackagesUpdateParameters.class);\r
79     myMockProcess = m.mock(BuildProcess.class);\r
80     myNuGet = m.mock(NuGetFetchParameters.class);\r
81 \r
82     m.checking(new Expectations() {{\r
83       allowing(myContext).getBuild();\r
84       will(returnValue(myBuild));\r
85       allowing(myBuild).getBuildLogger();\r
86       will(returnValue(myLogger));\r
87       allowing(myBuild).getCheckoutDirectory();\r
88       will(returnValue(myRoot));\r
89 \r
90       allowing(myMockProcess).start();\r
91       allowing(myMockProcess).waitFor();\r
92       will(returnValue(BuildFinishedStatus.FINISHED_SUCCESS));\r
93 \r
94       allowing(myLogger).message(with(any(String.class)));\r
95       allowing(myLogger).activityStarted(with(equal("install")), with(any(String.class)), with(any(String.class)));\r
96       allowing(myLogger).activityFinished(with(equal("install")), with(any(String.class)));\r
97 \r
98       allowing(myInstall).getNuGetParameters();\r
99       will(returnValue(myNuGet));\r
100       allowing(myUpdate).getNuGetParameters();\r
101       will(returnValue(myNuGet));\r
102     }});\r
103   }\r
104 \r
105   @Test\r
106   public void test_01_online_sources() throws RunBuildException {\r
107     ArchiveUtil.unpackZip(getTestDataPath("test-01.zip"), "", myRoot);\r
108 \r
109     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, false,\r
110             Arrays.asList(\r
111                     new PackageInfo("Machine.Specifications", "0.4.13.0"),\r
112                     new PackageInfo("NUnit", "2.5.7.10213"),\r
113                     new PackageInfo("Ninject", "2.2.1.4"))\r
114     );\r
115 \r
116     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
117     System.out.println("installed packageses = " + packageses);\r
118 \r
119     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
120     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
121     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
122     Assert.assertEquals(4, packageses.size());\r
123   }\r
124 \r
125   @NotNull\r
126   private File getTestDataPath(final String path) {\r
127     return Paths.getTestDataPath("integration/" + path);\r
128   }\r
129 \r
130   @Test\r
131   public void test_01_online_sources_update_forConfig() throws RunBuildException {\r
132     ArchiveUtil.unpackZip(getTestDataPath("test-01.zip"), "", myRoot);\r
133 \r
134     m.checking(new Expectations() {{\r
135       allowing(myLogger).activityStarted(with(equal("update")), with(any(String.class)), with(equal("nuget")));\r
136       allowing(myLogger).activityFinished(with(equal("update")), with(equal("nuget")));\r
137 \r
138       allowing(myUpdate).getUseSafeUpdate(); will(returnValue(false));\r
139       allowing(myUpdate).getPackagesToUpdate(); will(returnValue(Collections.<String>emptyList()));\r
140       allowing(myUpdate).getUpdateMode(); will(returnValue(PackagesUpdateMode.FOR_EACH_PACKAGES_CONFIG));\r
141     }});\r
142 \r
143     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, true, null);\r
144 \r
145 \r
146     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
147     System.out.println("installed packageses = " + packageses);\r
148 \r
149     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
150     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.10.11092").isDirectory());\r
151     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
152     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
153     Assert.assertEquals(5, packageses.size());\r
154   }\r
155 \r
156   @Test\r
157   public void test_01_online_sources_update_forSln() throws RunBuildException {\r
158     ArchiveUtil.unpackZip(getTestDataPath("test-01.zip"), "", myRoot);\r
159 \r
160     m.checking(new Expectations() {{\r
161       allowing(myLogger).activityStarted(with(equal("update")), with(any(String.class)), with(equal("nuget")));\r
162       allowing(myLogger).activityFinished(with(equal("update")), with(equal("nuget")));\r
163 \r
164       allowing(myUpdate).getUseSafeUpdate();\r
165       will(returnValue(false));\r
166       allowing(myUpdate).getPackagesToUpdate();\r
167       will(returnValue(Collections.<String>emptyList()));\r
168       allowing(myUpdate).getUpdateMode();\r
169       will(returnValue(PackagesUpdateMode.FOR_SLN));\r
170     }});\r
171 \r
172     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, true, null);\r
173 \r
174 \r
175     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
176     System.out.println("installed packageses = " + packageses);\r
177 \r
178     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
179     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.10.11092").isDirectory());\r
180     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
181     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
182     Assert.assertEquals(5, packageses.size());\r
183   }\r
184 \r
185   @Test\r
186   public void test_01_online_sources_update_safe() throws RunBuildException {\r
187     ArchiveUtil.unpackZip(getTestDataPath("test-01.zip"), "", myRoot);\r
188 \r
189     m.checking(new Expectations() {{\r
190       allowing(myLogger).activityStarted(with(equal("update")), with(any(String.class)), with(equal("nuget")));\r
191       allowing(myLogger).activityFinished(with(equal("update")), with(equal("nuget")));\r
192 \r
193       allowing(myUpdate).getUseSafeUpdate();\r
194       will(returnValue(true));\r
195       allowing(myUpdate).getPackagesToUpdate();\r
196       will(returnValue(Collections.<String>emptyList()));\r
197       allowing(myUpdate).getUpdateMode();\r
198       will(returnValue(PackagesUpdateMode.FOR_EACH_PACKAGES_CONFIG));\r
199     }});\r
200 \r
201     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), false, true, null);\r
202 \r
203 \r
204     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
205     System.out.println("installed packageses = " + packageses);\r
206 \r
207     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
208     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.10.11092").isDirectory());\r
209     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
210     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
211     Assert.assertEquals(5, packageses.size());\r
212   }\r
213 \r
214   @Test\r
215   public void test_01_online_sources_ecludeVersion() throws RunBuildException {\r
216     ArchiveUtil.unpackZip(getTestDataPath("test-01.zip"), "", myRoot);\r
217 \r
218     fetchPackages(new File(myRoot, "sln1-lib.sln"), Collections.<String>emptyList(), true, false,\r
219             Arrays.asList(\r
220                     new PackageInfo("Machine.Specifications", "0.4.13.0"),\r
221                     new PackageInfo("NUnit", "2.5.7.10213"),\r
222                     new PackageInfo("Ninject", "2.2.1.4")));\r
223 \r
224     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
225     System.out.println("installed packageses = " + packageses);\r
226 \r
227     Assert.assertTrue(new File(myRoot, "packages/NUnit").isDirectory());\r
228     Assert.assertTrue(new File(myRoot, "packages/NInject").isDirectory());\r
229     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications").isDirectory());\r
230     Assert.assertEquals(4, packageses.size());\r
231   }\r
232 \r
233   @Test(enabled = false, dependsOnGroups = "Need to understand how to check NuGet uses only specified sources")\r
234   public void test_01_local_sources() throws RunBuildException {\r
235     ArchiveUtil.unpackZip(getTestDataPath("test-01.zip"), "", myRoot);\r
236     File sourcesDir = new File(myRoot, "js");\r
237     ArchiveUtil.unpackZip(Paths.getTestDataPath("test-01-sources.zip"), "", sourcesDir);\r
238 \r
239     fetchPackages(new File(myRoot, "sln1-lib.sln"), Arrays.asList("file:///" + sourcesDir.getPath()), false, false, null);\r
240 \r
241     List<File> packageses = Arrays.asList(new File(myRoot, "packages").listFiles());\r
242     System.out.println("installed packageses = " + packageses);\r
243 \r
244     Assert.assertTrue(new File(myRoot, "packages/NUnit.2.5.7.10213").isDirectory());\r
245     Assert.assertTrue(new File(myRoot, "packages/NInject.2.2.1.4").isDirectory());\r
246     Assert.assertTrue(new File(myRoot, "packages/Machine.Specifications.0.4.13.0").isDirectory());\r
247     Assert.assertEquals(4, packageses.size());\r
248   }\r
249 \r
250   private void fetchPackages(final File sln,\r
251                              final List<String> sources,\r
252                              final boolean excludeVersion,\r
253                              final boolean update,\r
254                              @Nullable Collection<PackageInfo> detectedPackages) throws RunBuildException {\r
255 \r
256     m.checking(new Expectations() {{\r
257       allowing(myParametersFactory).loadNuGetFetchParameters(myContext);\r
258       will(returnValue(myNuGet));\r
259       allowing(myParametersFactory).loadInstallPackagesParameters(myContext, myNuGet);\r
260       will(returnValue(myInstall));\r
261 \r
262       allowing(myNuGet).getNuGetExeFile();\r
263       will(returnValue(Paths.getPathToNuGet()));\r
264       allowing(myNuGet).getSolutionFile();\r
265       will(returnValue(sln));\r
266       allowing(myNuGet).getNuGetPackageSources();\r
267       will(returnValue(sources));\r
268       allowing(myInstall).getExcludeVersion();\r
269       will(returnValue(excludeVersion));\r
270       allowing(myParametersFactory).loadUpdatePackagesParameters(myContext, myNuGet);\r
271       will(returnValue(update ? myUpdate : null));\r
272     }});\r
273 \r
274     NuGetPackagesCollectorImpl collector = new NuGetPackagesCollectorImpl();\r
275     PackageUsages pu = new PackageUsagesImpl(\r
276             collector,\r
277             new NuGetPackagesConfigParser()\r
278     );\r
279 \r
280     BuildProcess proc = new PackagesInstallerRunner(\r
281             new NuGetActionFactoryImpl(executingFactory(), pu),\r
282             myParametersFactory\r
283     ).createBuildProcess(myBuild, myContext);\r
284 \r
285     assertRunSuccessfully(proc, BuildFinishedStatus.FINISHED_SUCCESS);\r
286 \r
287     System.out.println(collector.getPackages());\r
288     if (detectedPackages != null) {\r
289       Assert.assertEquals(\r
290               new TreeSet<PackageInfo>(collector.getPackages().getPackages()),\r
291               new TreeSet<PackageInfo>(detectedPackages));\r
292     }\r
293 \r
294     m.assertIsSatisfied();\r
295   }\r
296 \r
297   private CommandlineBuildProcessFactory executingFactory() {\r
298     return new CommandlineBuildProcessFactory() {\r
299       public BuildProcess executeCommandLine(@NotNull final BuildRunnerContext hostContext,\r
300                                              @NotNull final File program,\r
301                                              @NotNull final Collection<String> argz,\r
302                                              @NotNull final File workingDir) throws RunBuildException {\r
303         return new BuildProcessBase() {\r
304           @NotNull\r
305           @Override\r
306           protected BuildFinishedStatus waitForImpl() throws RunBuildException {\r
307             GeneralCommandLine cmd = new GeneralCommandLine();\r
308             cmd.setExePath(program.getPath());\r
309             for (String arg : argz) {\r
310               cmd.addParameter(arg);\r
311             }\r
312             cmd.setWorkingDirectory(workingDir);\r
313 \r
314             System.out.println("Run: " + cmd.getCommandLineString());\r
315 \r
316             ExecResult result = SimpleCommandLineProcessRunner.runCommand(cmd, new byte[0]);\r
317 \r
318             System.out.println(result.getStdout());\r
319             System.out.println(result.getStderr());\r
320 \r
321             return result.getExitCode() == 0\r
322                     ? BuildFinishedStatus.FINISHED_SUCCESS\r
323                     : BuildFinishedStatus.FINISHED_FAILED;\r
324           }\r
325         };\r
326       }\r
327     };\r
328   }\r
329 }\r