1
1
using System . IO ;
2
2
using System . Collections . Generic ;
3
- using System . Threading . Tasks ;
4
3
using Microsoft . Android . Build . BaseTasks . Tests . Utilities ;
5
4
using Microsoft . Android . Build . Tasks ;
6
5
using NUnit . Framework ;
7
6
using Microsoft . Build . Framework ;
8
- using Xamarin . Build ;
7
+ using NUnit . Framework . Internal ;
8
+ using System . Linq ;
9
9
10
10
namespace Microsoft . Android . Build . BaseTasks . Tests
11
11
{
12
12
[ TestFixture ]
13
13
public class AndroidToolTaskTests
14
14
{
15
- public class MyAndroidTask : AndroidTask {
15
+ List < BuildErrorEventArgs > errors ;
16
+ List < BuildWarningEventArgs > warnings ;
17
+ List < BuildMessageEventArgs > messages ;
18
+ MockBuildEngine engine ;
19
+
20
+ public class MyAndroidTask : AndroidTask
21
+ {
16
22
public override string TaskPrefix { get ; } = "MAT" ;
17
23
public string Key { get ; set ; }
18
24
public string Value { get ; set ; }
@@ -25,7 +31,8 @@ public override bool RunTask ()
25
31
}
26
32
}
27
33
28
- public class MyOtherAndroidTask : AndroidTask {
34
+ public class MyOtherAndroidTask : AndroidTask
35
+ {
29
36
public override string TaskPrefix { get ; } = "MOAT" ;
30
37
public string Key { get ; set ; }
31
38
public bool ProjectSpecific { get ; set ; } = false ;
@@ -40,15 +47,31 @@ public override bool RunTask ()
40
47
}
41
48
}
42
49
50
+ public class DotnetToolOutputTestTask : AndroidToolTask
51
+ {
52
+ public override string TaskPrefix { get ; } = "DTOT" ;
53
+ protected override string ToolName => "dotnet" ;
54
+ protected override string GenerateFullPathToTool ( ) => ToolExe ;
55
+ public string CommandLineArgs { get ; set ; } = "--info" ;
56
+ protected override string GenerateCommandLineCommands ( ) => CommandLineArgs ;
57
+ }
58
+
59
+ [ SetUp ]
60
+ public void TestSetup ( )
61
+ {
62
+ errors = new List < BuildErrorEventArgs > ( ) ;
63
+ warnings = new List < BuildWarningEventArgs > ( ) ;
64
+ messages = new List < BuildMessageEventArgs > ( ) ;
65
+ engine = new MockBuildEngine ( TestContext . Out , errors , warnings , messages ) ;
66
+ }
67
+
43
68
[ Test ]
44
69
[ TestCase ( true , true , true ) ]
45
70
[ TestCase ( false , false , true ) ]
46
71
[ TestCase ( true , false , false ) ]
47
72
[ TestCase ( false , true , false ) ]
48
73
public void TestRegisterTaskObjectCanRetrieveCorrectItem ( bool projectSpecificA , bool projectSpecificB , bool expectedResult )
49
74
{
50
- var engine = new MockBuildEngine ( TestContext . Out ) {
51
- } ;
52
75
var task = new MyAndroidTask ( ) {
53
76
BuildEngine = engine ,
54
77
Key = "Foo" ,
@@ -72,8 +95,6 @@ public void TestRegisterTaskObjectCanRetrieveCorrectItem (bool projectSpecificA,
72
95
[ TestCase ( false , true , false ) ]
73
96
public void TestRegisterTaskObjectFailsWhenDirectoryChanges ( bool projectSpecificA , bool projectSpecificB , bool expectedResult )
74
97
{
75
- var engine = new MockBuildEngine ( TestContext . Out ) {
76
- } ;
77
98
MyAndroidTask task ;
78
99
var currentDir = Directory . GetCurrentDirectory ( ) ;
79
100
Directory . SetCurrentDirectory ( Path . Combine ( currentDir , ".." ) ) ;
@@ -96,5 +117,30 @@ public void TestRegisterTaskObjectFailsWhenDirectoryChanges (bool projectSpecifi
96
117
task2 . Execute ( ) ;
97
118
Assert . AreEqual ( expectedResult , string . Compare ( task2 . Value , task . Value , ignoreCase : true ) == 0 ) ;
98
119
}
120
+
121
+ [ Test ]
122
+ [ TestCase ( "invalidcommand" , false , "You intended to execute a .NET program, but dotnet-invalidcommand does not exist." ) ]
123
+ [ TestCase ( "--info" , true , "" ) ]
124
+ public void FailedAndroidToolTaskShouldLogOutputAsError ( string args , bool expectedResult , string expectedErrorText )
125
+ {
126
+ var task = new DotnetToolOutputTestTask ( ) {
127
+ BuildEngine = engine ,
128
+ CommandLineArgs = args ,
129
+ } ;
130
+ var taskSucceeded = task . Execute ( ) ;
131
+ Assert . AreEqual ( expectedResult , taskSucceeded , "Task execution did not return expected value." ) ;
132
+
133
+ if ( taskSucceeded ) {
134
+ Assert . IsEmpty ( errors , "Successful task should not have any errors." ) ;
135
+ } else {
136
+ Assert . IsNotEmpty ( errors , "Task expected to fail should have errors." ) ;
137
+ Assert . AreEqual ( "MSB6006" , errors [ 0 ] . Code ,
138
+ $ "Expected error code MSB6006 but got { errors [ 0 ] . Code } ") ;
139
+ Assert . AreEqual ( "XADTOT0000" , errors [ 1 ] . Code ,
140
+ $ "Expected error code XADTOT0000 but got { errors [ 1 ] . Code } ") ;
141
+ Assert . IsTrue ( errors . Any ( e => e . Message . Contains ( expectedErrorText ) ) ,
142
+ "Task expected to fail should contain expected error text." ) ;
143
+ }
144
+ }
99
145
}
100
146
}
0 commit comments