1+ // Copyright (c) Microsoft Corporation.
2+ // Licensed under the MIT License.
3+
4+ using Azure . Sdk . Tools . Cli . SampleGeneration . Languages ;
5+
6+ namespace Azure . Sdk . Tools . Cli . Tests . SampleGeneration ;
7+
8+ public class PythonSourceInputProviderTests
9+ {
10+ private static string CreateTempPackage ( )
11+ {
12+ string root = Path . Combine ( Path . GetTempPath ( ) , "azsdk-python-test-" + Guid . NewGuid ( ) . ToString ( "N" ) ) ;
13+ Directory . CreateDirectory ( root ) ;
14+ return root ;
15+ }
16+
17+ private static void WriteFile ( string dir , string name , string content )
18+ {
19+ var path = Path . Combine ( dir , name ) ;
20+ Directory . CreateDirectory ( Path . GetDirectoryName ( path ) ! ) ;
21+ File . WriteAllText ( path , content ) ;
22+ }
23+
24+ [ Test ]
25+ public void Includes_Azure_Directory_When_Present ( )
26+ {
27+ var provider = new PythonSourceInputProvider ( ) ;
28+ var root = CreateTempPackage ( ) ;
29+ var azureDir = Path . Combine ( root , "azure" ) ;
30+ Directory . CreateDirectory ( azureDir ) ;
31+ WriteFile ( azureDir , "client.py" , "class Client: pass" ) ;
32+
33+ var inputs = provider . Create ( root ) ;
34+
35+ Assert . Multiple ( ( ) =>
36+ {
37+ Assert . That ( inputs . Count , Is . EqualTo ( 1 ) , "Should have exactly one input when only azure directory exists" ) ;
38+ Assert . That ( inputs [ 0 ] . Path , Is . EqualTo ( azureDir ) , "Should include azure directory" ) ;
39+ Assert . That ( inputs [ 0 ] . IncludeExtensions , Is . Not . Null , "Should have include extensions specified" ) ;
40+ Assert . That ( inputs [ 0 ] . IncludeExtensions , Does . Contain ( ".py" ) , "Should include .py files" ) ;
41+ } ) ;
42+ }
43+
44+ [ Test ]
45+ public void Throws_When_Azure_Directory_Missing ( )
46+ {
47+ var provider = new PythonSourceInputProvider ( ) ;
48+ var root = CreateTempPackage ( ) ;
49+
50+ var ex = Assert . Throws < ArgumentException > ( ( ) => provider . Create ( root ) ) ;
51+ Assert . Multiple ( ( ) =>
52+ {
53+ Assert . That ( ex ! . Message , Does . Contain ( "azure" ) , "Error message should mention azure directory" ) ;
54+ Assert . That ( ex . Message , Does . Contain ( root ) , "Error message should include the provided path" ) ;
55+ Assert . That ( ex . ParamName , Is . EqualTo ( "packagePath" ) , "Should specify packagePath as the problem parameter" ) ;
56+ } ) ;
57+ }
58+
59+ [ Test ]
60+ public void Includes_Samples_Directory_When_Present ( )
61+ {
62+ var provider = new PythonSourceInputProvider ( ) ;
63+ var root = CreateTempPackage ( ) ;
64+ var azureDir = Path . Combine ( root , "azure" ) ;
65+ var samplesDir = Path . Combine ( root , "samples" ) ;
66+ Directory . CreateDirectory ( azureDir ) ;
67+ Directory . CreateDirectory ( samplesDir ) ;
68+ WriteFile ( azureDir , "client.py" , "class Client: pass" ) ;
69+ WriteFile ( samplesDir , "sample1.py" , "# Sample code" ) ;
70+
71+ var inputs = provider . Create ( root ) ;
72+
73+ Assert . Multiple ( ( ) =>
74+ {
75+ Assert . That ( inputs , Has . Count . EqualTo ( 2 ) , "Should include both azure and samples directories" ) ;
76+ Assert . That ( inputs . Any ( i => i . Path == azureDir ) , "Should include azure directory" ) ;
77+ Assert . That ( inputs . Any ( i => i . Path == samplesDir ) , "Should include samples directory" ) ;
78+
79+ var samplesInput = inputs . First ( i => i . Path == samplesDir ) ;
80+ Assert . That ( samplesInput . IncludeExtensions , Does . Contain ( ".py" ) , "Samples directory should include .py files" ) ;
81+ } ) ;
82+ }
83+
84+ [ Test ]
85+ public void Works_Without_Samples_Directory ( )
86+ {
87+ var provider = new PythonSourceInputProvider ( ) ;
88+ var root = CreateTempPackage ( ) ;
89+ var azureDir = Path . Combine ( root , "azure" ) ;
90+ Directory . CreateDirectory ( azureDir ) ;
91+ WriteFile ( azureDir , "client.py" , "class Client: pass" ) ;
92+
93+ var inputs = provider . Create ( root ) ;
94+
95+ Assert . Multiple ( ( ) =>
96+ {
97+ Assert . That ( inputs . Count , Is . EqualTo ( 1 ) , "Should work without samples directory" ) ;
98+ Assert . That ( inputs [ 0 ] . Path , Is . EqualTo ( azureDir ) , "Should still include azure directory" ) ;
99+ } ) ;
100+ }
101+
102+ [ Test ]
103+ public void Includes_Test_Resources_Files_From_Parent_Directory ( )
104+ {
105+ var provider = new PythonSourceInputProvider ( ) ;
106+ var tempRoot = Path . GetTempPath ( ) ;
107+ var parentDir = Path . Combine ( tempRoot , "azsdk-python-parent-" + Guid . NewGuid ( ) . ToString ( "N" ) ) ;
108+ var packageDir = Path . Combine ( parentDir , "package" ) ;
109+ Directory . CreateDirectory ( packageDir ) ;
110+
111+ var azureDir = Path . Combine ( packageDir , "azure" ) ;
112+ Directory . CreateDirectory ( azureDir ) ;
113+ WriteFile ( azureDir , "client.py" , "class Client: pass" ) ;
114+
115+ var testResourcesFile1 = Path . Combine ( parentDir , "test-resources.json" ) ;
116+ var testResourcesFile2 = Path . Combine ( parentDir , "test-resources-post.ps1" ) ;
117+ File . WriteAllText ( testResourcesFile1 , "{}" ) ;
118+ File . WriteAllText ( testResourcesFile2 , "{}" ) ;
119+
120+ var inputs = provider . Create ( packageDir ) ;
121+
122+ Assert . Multiple ( ( ) =>
123+ {
124+ Assert . That ( inputs , Has . Count . EqualTo ( 3 ) , "Should include azure dir + 2 test-resources files" ) ;
125+ Assert . That ( inputs . Any ( i => i . Path == azureDir ) , "Should include azure directory" ) ;
126+ Assert . That ( inputs . Any ( i => i . Path == testResourcesFile1 ) , "Should include test-resources.json" ) ;
127+ Assert . That ( inputs . Any ( i => i . Path == testResourcesFile2 ) , "Should include test-resources-dev.json" ) ;
128+ } ) ;
129+ }
130+
131+ [ Test ]
132+ public void Works_Without_Test_Resources_Files ( )
133+ {
134+ var provider = new PythonSourceInputProvider ( ) ;
135+ var root = CreateTempPackage ( ) ;
136+ var azureDir = Path . Combine ( root , "azure" ) ;
137+ Directory . CreateDirectory ( azureDir ) ;
138+ WriteFile ( azureDir , "client.py" , "class Client: pass" ) ;
139+
140+ var inputs = provider . Create ( root ) ;
141+
142+ Assert . Multiple ( ( ) =>
143+ {
144+ Assert . That ( inputs . Count , Is . EqualTo ( 1 ) , "Should work without test-resources files" ) ;
145+ Assert . That ( inputs [ 0 ] . Path , Is . EqualTo ( azureDir ) , "Should still include azure directory" ) ;
146+ } ) ;
147+ }
148+
149+ [ Test ]
150+ public void Works_When_Package_Has_No_Parent_Directory ( )
151+ {
152+ var provider = new PythonSourceInputProvider ( ) ;
153+ var root = CreateTempPackage ( ) ;
154+ var azureDir = Path . Combine ( root , "azure" ) ;
155+ Directory . CreateDirectory ( azureDir ) ;
156+ WriteFile ( azureDir , "client.py" , "class Client: pass" ) ;
157+
158+ var inputs = provider . Create ( root ) ;
159+
160+ Assert . Multiple ( ( ) =>
161+ {
162+ Assert . That ( inputs , Has . Count . EqualTo ( 1 ) , "Should work when package has no parent directory or no test-resources files" ) ;
163+ Assert . That ( inputs [ 0 ] . Path , Is . EqualTo ( azureDir ) , "Should still include azure directory" ) ;
164+ } ) ;
165+ }
166+
167+ [ Test ]
168+ public void Includes_All_Components_When_Everything_Present ( )
169+ {
170+ var provider = new PythonSourceInputProvider ( ) ;
171+ var tempRoot = Path . GetTempPath ( ) ;
172+ var parentDir = Path . Combine ( tempRoot , "azsdk-python-full-" + Guid . NewGuid ( ) . ToString ( "N" ) ) ;
173+ var packageDir = Path . Combine ( parentDir , "package" ) ;
174+ Directory . CreateDirectory ( packageDir ) ;
175+
176+ var azureDir = Path . Combine ( packageDir , "azure" ) ;
177+ Directory . CreateDirectory ( azureDir ) ;
178+ WriteFile ( azureDir , "client.py" , "class Client: pass" ) ;
179+ WriteFile ( azureDir , "models.py" , "class Model: pass" ) ;
180+
181+ var samplesDir = Path . Combine ( packageDir , "samples" ) ;
182+ Directory . CreateDirectory ( samplesDir ) ;
183+ WriteFile ( samplesDir , "sample1.py" , "# Sample 1" ) ;
184+ WriteFile ( samplesDir , "sample2.py" , "# Sample 2" ) ;
185+
186+ var testResourcesFile = Path . Combine ( parentDir , "test-resources.json" ) ;
187+ File . WriteAllText ( testResourcesFile , "{}" ) ;
188+
189+ var inputs = provider . Create ( packageDir ) ;
190+
191+ Assert . Multiple ( ( ) =>
192+ {
193+ Assert . That ( inputs , Has . Count . EqualTo ( 3 ) , "Should include azure dir + samples dir + test-resources file" ) ;
194+ Assert . That ( inputs . Any ( i => i . Path == azureDir ) , "Should include azure directory" ) ;
195+ Assert . That ( inputs . Any ( i => i . Path == samplesDir ) , "Should include samples directory" ) ;
196+ Assert . That ( inputs . Any ( i => i . Path == testResourcesFile ) , "Should include test-resources file" ) ;
197+
198+ var directoryInputs = inputs . Where ( i => Directory . Exists ( i . Path ) ) ;
199+ Assert . That ( directoryInputs . All ( i => i . IncludeExtensions != null && i . IncludeExtensions . Contains ( ".py" ) ) ,
200+ "All directory inputs should include .py files" ) ;
201+ } ) ;
202+ }
203+ }
0 commit comments