Skip to content

Commit f3e05de

Browse files
Remove disk IO from JcwJavaSourceGenerator tests
Refactor tests to avoid temp directory creation and cleanup: - Filtering test now checks the scanned peer list directly - JNI name validation tests call ValidateJniName directly - Remove CreateTempDir/DeleteTempDir helpers and the empty catch block - Remove Generate_CreatesCorrectFileStructure integration test All content generation tests already use in-memory StringWriter. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0a332e2 commit f3e05de

2 files changed

Lines changed: 11 additions & 52 deletions

File tree

tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/FixtureTestBase.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,4 @@ private protected static List<string> GetMemberRefNames (MetadataReader reader)
125125
.Select (i => reader.GetMemberReference (MetadataTokens.MemberReferenceHandle (i)))
126126
.Select (m => reader.GetString (m.Name))
127127
.ToList ();
128-
129-
private protected static string CreateTempDir ()
130-
{
131-
var dir = Path.Combine (Path.GetTempPath (), $"typemap-test-{Guid.NewGuid ():N}");
132-
Directory.CreateDirectory (dir);
133-
return dir;
134-
}
135-
136-
private protected static void DeleteTempDir (string dir)
137-
{
138-
if (Directory.Exists (dir)) {
139-
try {
140-
Directory.Delete (dir, true);
141-
} catch (IOException) {
142-
}
143-
}
144-
}
145128
}

tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/JcwJavaSourceGeneratorTests.cs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,17 @@ public void JniTypeToJava_ConvertsCorrectly (string jniType, string expected)
7070

7171
}
7272

73-
public class Filtering : IDisposable
73+
public class Filtering
7474
{
75-
readonly string _outputDir = CreateTempDir ();
76-
public void Dispose () => DeleteTempDir (_outputDir);
7775

7876
[Fact]
7977
public void Generate_SkipsMcwTypes ()
8078
{
8179
var peers = ScanFixtures ();
82-
var generator = new JcwJavaSourceGenerator ();
83-
var files = generator.Generate (peers, _outputDir);
84-
Assert.DoesNotContain (files, f => f.EndsWith ("java/lang/Object.java"));
85-
Assert.DoesNotContain (files, f => f.EndsWith ("android/app/Activity.java"));
86-
Assert.Contains (files, f => f.Replace ('\\', '/').Contains ("my/app/MainActivity.java"));
80+
var acwTypes = peers.Where (p => !p.DoNotGenerateAcw && !p.IsInterface).ToList ();
81+
Assert.DoesNotContain (acwTypes, p => p.JavaName == "java/lang/Object");
82+
Assert.DoesNotContain (acwTypes, p => p.JavaName == "android/app/Activity");
83+
Assert.Contains (acwTypes, p => p.JavaName == "my/app/MainActivity");
8784
}
8885

8986
}
@@ -251,25 +248,8 @@ public void Generate_NestedType_HasCorrectPackageAndClassName ()
251248

252249
}
253250

254-
public class OutputFilePath : IDisposable
251+
public class JniNameValidation
255252
{
256-
readonly string _outputDir = CreateTempDir ();
257-
public void Dispose () => DeleteTempDir (_outputDir);
258-
259-
[Fact]
260-
public void Generate_CreatesCorrectFileStructure ()
261-
{
262-
var peers = ScanFixtures ();
263-
var generator = new JcwJavaSourceGenerator ();
264-
var files = generator.Generate (peers, _outputDir);
265-
Assert.NotEmpty (files);
266-
267-
foreach (var file in files) {
268-
Assert.StartsWith (_outputDir, file);
269-
Assert.True (File.Exists (file), $"Generated file should exist: {file}");
270-
Assert.EndsWith (".java", file);
271-
}
272-
}
273253

274254
[Theory]
275255
[InlineData ("")]
@@ -282,11 +262,9 @@ public void Generate_CreatesCorrectFileStructure ()
282262
[InlineData ("C:\\Windows\\System32")]
283263
[InlineData ("com/Ex:ample")]
284264
[InlineData ("/absolute/path")]
285-
public void Generate_InvalidJniName_Throws (string badJniName)
265+
public void ValidateJniName_InvalidName_Throws (string badJniName)
286266
{
287-
var peer = MakeAcwPeer (badJniName, "Test.Bad", "TestApp");
288-
var generator = new JcwJavaSourceGenerator ();
289-
Assert.Throws<ArgumentException> (() => generator.Generate (new [] { peer }, _outputDir));
267+
Assert.Throws<ArgumentException> (() => JniSignatureHelper.ValidateJniName (badJniName));
290268
}
291269

292270
[Theory]
@@ -295,11 +273,9 @@ public void Generate_InvalidJniName_Throws (string badJniName)
295273
[InlineData ("SingleSegment")]
296274
[InlineData ("com/example/_Private")]
297275
[InlineData ("com/example/$Generated")]
298-
public void Generate_ValidJniName_DoesNotThrow (string validJniName)
276+
public void ValidateJniName_ValidName_DoesNotThrow (string validJniName)
299277
{
300-
var peer = MakeAcwPeer (validJniName, "Test.Valid", "TestApp");
301-
var generator = new JcwJavaSourceGenerator ();
302-
generator.Generate (new [] { peer }, _outputDir);
278+
JniSignatureHelper.ValidateJniName (validJniName);
303279
}
304280

305281
}
@@ -330,4 +306,4 @@ public void Generate_TouchHandler_HasExpectedMethodSignatures ()
330306
}
331307

332308
}
333-
}
309+
}

0 commit comments

Comments
 (0)