Skip to content

Commit 0a107ae

Browse files
committed
add initial prototype
1 parent 803c0d0 commit 0a107ae

17 files changed

Lines changed: 3105 additions & 0 deletions

tools/azsdk-cli/Azure.Sdk.Tools.Cli.Tests/Helpers/FileHelperTests.cs

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Text;
5+
using Azure.Sdk.Tools.Cli.SampleGeneration.Languages;
6+
7+
namespace Azure.Sdk.Tools.Cli.Tests.SampleGeneration;
8+
9+
public class DotNetSourceInputProviderTests
10+
{
11+
private static string CreateTempPackage(out string srcDir, out string testsDir)
12+
{
13+
string root = Path.Combine(Path.GetTempPath(), "azsdk-test-" + Guid.NewGuid().ToString("N"));
14+
Directory.CreateDirectory(root);
15+
srcDir = Path.Combine(root, "src");
16+
testsDir = Path.Combine(root, "tests");
17+
Directory.CreateDirectory(srcDir);
18+
Directory.CreateDirectory(testsDir);
19+
return root;
20+
}
21+
22+
private static string WriteFile(string dir, string name, string content)
23+
{
24+
var path = Path.Combine(dir, name);
25+
File.WriteAllText(path, content, Encoding.UTF8);
26+
return path;
27+
}
28+
29+
[Test]
30+
public void Includes_Infrastructure_Base_Class_No_Test_Methods()
31+
{
32+
var provider = new DotNetSourceInputProvider();
33+
var root = CreateTempPackage(out var src, out var tests);
34+
35+
WriteFile(src, "Client.cs", "public class Client { }");
36+
WriteFile(tests, "KeysTestBase.cs", @"using NUnit.Framework; public abstract class KeysTestBase { [SetUp] public void Setup(){} }");
37+
38+
var inputs = provider.Create(root);
39+
40+
Assert.Multiple(() =>
41+
{
42+
// Expect: src folder and KeysTestBase.cs represented (as individual file input), no test methods so it's infra.
43+
Assert.That(inputs.Any(i => i.Path == src), "src directory should be included");
44+
Assert.That(inputs.Any(i => i.Path.EndsWith("KeysTestBase.cs")), "KeysTestBase.cs should be included as infra");
45+
});
46+
47+
}
48+
49+
[Test]
50+
public void Excludes_File_With_Test_Method()
51+
{
52+
var provider = new DotNetSourceInputProvider();
53+
var root = CreateTempPackage(out var src, out var tests);
54+
55+
WriteFile(src, "Client.cs", "public class Client { }");
56+
WriteFile(tests, "KeyClientTests.cs", @"using NUnit.Framework; public class KeyClientTests { [Test] public void TestA(){} }");
57+
58+
var inputs = provider.Create(root);
59+
60+
Assert.That(!inputs.Any(i => i.Path.EndsWith("KeyClientTests.cs")), "Test file with [Test] should be excluded");
61+
}
62+
63+
[Test]
64+
public void Includes_File_With_Lifecycle_Only()
65+
{
66+
var provider = new DotNetSourceInputProvider();
67+
var root = CreateTempPackage(out var src, out var tests);
68+
69+
WriteFile(src, "Client.cs", "public class Client { }");
70+
WriteFile(tests, "Fixture.cs", @"using NUnit.Framework; public class Fixture { [OneTimeSetUp] public void Init(){} }");
71+
72+
var inputs = provider.Create(root);
73+
74+
Assert.That(inputs.Any(i => i.Path.EndsWith("Fixture.cs")), "File with only lifecycle attributes should be treated as infra");
75+
}
76+
77+
[Test]
78+
public void Excludes_File_With_TestCase_Method()
79+
{
80+
var provider = new DotNetSourceInputProvider();
81+
var root = CreateTempPackage(out var src, out var tests);
82+
83+
WriteFile(src, "Client.cs", "public class Client { }");
84+
WriteFile(tests, "SomethingTests.cs", @"using NUnit.Framework; public class SomethingTests { [TestCase(1)] public void TestA(int x){} }");
85+
86+
var inputs = provider.Create(root);
87+
Assert.That(!inputs.Any(i => i.Path.EndsWith("SomethingTests.cs")), "File containing [TestCase] should be excluded");
88+
}
89+
90+
[Test]
91+
public void Includes_Abstract_Base_With_Inheritance_Of_RecordedTestBase()
92+
{
93+
var provider = new DotNetSourceInputProvider();
94+
var root = CreateTempPackage(out var src, out var tests);
95+
96+
WriteFile(src, "Client.cs", "public class Client { }");
97+
WriteFile(tests, "CustomTestBase.cs", @"public abstract class CustomTestBase : RecordedTestBase<object> { protected void Helper(){} }");
98+
99+
var inputs = provider.Create(root);
100+
Assert.That(inputs.Any(i => i.Path.EndsWith("CustomTestBase.cs")), "Abstract base inheriting RecordedTestBase should be included");
101+
}
102+
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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

Comments
 (0)