2424using System . Management . Automation ;
2525using Microsoft . Deployment . WindowsInstaller ;
2626using Microsoft . VisualStudio . TestTools . UnitTesting ;
27+ using System ;
28+ using System . Collections . Generic ;
2729
2830namespace Microsoft . Tools . WindowsInstaller . PowerShell . Commands
2931{
@@ -41,35 +43,15 @@ public class SourceCommandTests : TestBase
4143 [ TestMethod ]
4244 public void ModifyProductSource ( )
4345 {
44- PSObject obj = null ;
45-
46- // First attempt to see if there are any unmanaged products installed.
47- using ( var p = CreatePipeline ( "get-msiproductinfo -context userunmanaged" ) )
48- {
49- var output = p . Invoke ( ) ;
50- if ( null != output && 0 < output . Count ( ) )
51- {
52- obj = output [ 0 ] ;
53- }
54- else
55- {
56- Assert . Inconclusive ( "There are no unmanaged products installed with which to test source list modifications." ) ;
57- }
58- }
59-
60- // Retain the original source locations so we can attempt to restore it.
61- var product = obj . As < ProductInstallation > ( ) ;
62- var original = product . SourceList . ToArray ( ) ;
63-
64- try
46+ this . TestProductSources ( ( obj , product , original ) =>
6547 {
6648 using ( var p = CreatePipeline ( string . Format ( @"$Input | add-msisource -path '{0}' -passthru" , this . TestContext . DeploymentDirectory ) ) )
6749 {
6850 p . Input . Write ( obj ) ;
6951 var output = p . Invoke ( ) ;
7052
7153 Assert . IsNotNull ( output ) ;
72- Assert . AreEqual < int > ( original . Length + 1 , output . Count ( ) ) ;
54+ Assert . AreEqual ( original . Count + 1 , output . Count ( ) ) ;
7355 }
7456
7557 using ( var p = CreatePipeline ( @"$Input | add-msisource -path 'ShouldNotExist.txt' -passthru" ) )
@@ -89,8 +71,8 @@ public void ModifyProductSource()
8971
9072 // Should return the previous number of source locations we already registered.
9173 Assert . IsNotNull ( output ) ;
92- Assert . AreEqual < int > ( original . Length + 1 , output . Count ( ) ) ;
93- Assert . AreEqual < int > ( 1 , p . Error . Count ) ;
74+ Assert . AreEqual ( original . Count + 1 , output . Count ( ) ) ;
75+ Assert . AreEqual ( 1 , p . Error . Count ) ;
9476 }
9577
9678 using ( var p = CreatePipeline ( @"$Input | clear-msisource; $Input | get-msisource" ) )
@@ -101,17 +83,17 @@ public void ModifyProductSource()
10183 Assert . IsTrue ( null == output || 0 == output . Count ( ) ) ;
10284 }
10385
104- var paths = new string [ original . Length + 1 ] ;
86+ var paths = new string [ original . Count + 1 ] ;
10587 paths [ 0 ] = this . TestContext . DeploymentDirectory ;
10688 original . CopyTo ( paths , 1 ) ;
10789
108- using ( var p = CreatePipeline ( string . Format ( @"$Input | add-msisource '{0}' -passthru" , product . ProductCode ) ) )
90+ using ( var p = CreatePipeline ( string . Format ( @"$Input | add-msisource '{0}' -force - passthru" , product . ProductCode ) ) )
10991 {
11092 p . Input . Write ( paths , true ) ;
11193 var output = p . Invoke ( ) ;
11294
11395 Assert . IsNotNull ( output ) ;
114- Assert . AreEqual < int > ( paths . Length , output . Count ( ) ) ;
96+ Assert . AreEqual ( paths . Length , output . Count ( ) ) ;
11597 }
11698
11799 using ( var p = CreatePipeline ( string . Format ( @"$Input | remove-msisource -path '{0}' -passthru" , this . TestContext . DeploymentDirectory ) ) )
@@ -120,8 +102,110 @@ public void ModifyProductSource()
120102 var output = p . Invoke ( ) ;
121103
122104 Assert . IsNotNull ( output ) ;
123- Assert . AreEqual < int > ( original . Length , output . Count ( ) ) ;
105+ Assert . AreEqual ( original . Count , output . Count ( ) ) ;
106+ }
107+ } ) ;
108+ }
109+
110+ [ TestMethod ]
111+ public void AddSourceTestsPath ( )
112+ {
113+ this . TestProductSources ( ( obj , product , original ) =>
114+ {
115+ using ( var p = CreatePipeline ( @"$Input | add-msisource -path 'C:\ShouldNotExist\AddSourceTestsPath' -passthru" ) )
116+ {
117+ p . Input . Write ( obj ) ;
118+
119+ try
120+ {
121+ p . Invoke ( ) ;
122+ Assert . Fail ( "Expected CmdletInvocationException exception" ) ;
123+ }
124+ catch ( CmdletInvocationException )
125+ {
126+ return ;
127+ }
128+ catch ( Exception ex )
129+ {
130+ Assert . Fail ( "Expected CmdletInvocationException exception; caught {0} exception" , ex . GetType ( ) . Name ) ;
131+ }
132+ }
133+ } ) ;
134+ }
135+
136+ [ TestMethod ]
137+ public void AddSourceForceNoTestsPath ( )
138+ {
139+ this . TestProductSources ( ( obj , product , original ) =>
140+ {
141+ using ( var p = CreatePipeline ( @"$Input | add-msisource -path 'C:\ShouldNotExist\AddSourceForceNoTestsPath' -force -passthru" ) )
142+ {
143+ p . Input . Write ( obj ) ;
144+ var output = p . Invoke ( ) ;
145+
146+ Assert . IsFalse ( p . HadErrors ) ;
147+ Assert . IsNotNull ( output ) ;
148+ Assert . AreEqual ( original . Count + 1 , output . Count ( ) ) ;
124149 }
150+ } ) ;
151+ }
152+
153+ [ TestMethod ]
154+ public void RemoveSourceNoTestsPath ( )
155+ {
156+ this . TestProductSources ( ( obj , product , original ) =>
157+ {
158+ using ( var p = CreatePipeline ( @"$Input | remove-msisource -path 'C:\ShouldNotExist\RemoveSourceNoTestsPath' -passthru" ) )
159+ {
160+ p . Input . Write ( obj ) ;
161+ var output = p . Invoke ( ) ;
162+
163+ Assert . IsFalse ( p . HadErrors ) ;
164+ Assert . IsNotNull ( output ) ;
165+ Assert . AreEqual ( original . Count , output . Count ( ) ) ;
166+ }
167+ } ) ;
168+ }
169+
170+ private PSObject FindTestProduct ( )
171+ {
172+ using ( var p = CreatePipeline ( "get-msiproductinfo -context userunmanaged" ) )
173+ {
174+ var output = p . Invoke ( ) ;
175+ if ( null != output && 0 < output . Count ( ) )
176+ {
177+ return output [ 0 ] ;
178+ }
179+ }
180+
181+ return null ;
182+ }
183+
184+ private void TestProductSources ( Action < PSObject , ProductInstallation , ICollection < string > > action )
185+ {
186+ PSObject obj = null ;
187+
188+ // First attempt to see if there are any unmanaged products installed.
189+ using ( var p = CreatePipeline ( "get-msiproductinfo -context userunmanaged" ) )
190+ {
191+ var output = p . Invoke ( ) ;
192+ if ( null != output && 0 < output . Count ( ) )
193+ {
194+ obj = output [ 0 ] ;
195+ }
196+ else
197+ {
198+ Assert . Inconclusive ( "There are no unmanaged products installed with which to test source list modifications." ) ;
199+ }
200+ }
201+
202+ // Retain the original source locations so we can attempt to restore it.
203+ var product = obj . As < ProductInstallation > ( ) ;
204+ var original = product . SourceList . ToArray ( ) ;
205+
206+ try
207+ {
208+ action ( obj , product , original ) ;
125209 }
126210 finally
127211 {
0 commit comments