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