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