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