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