Skip to content

Commit 08f322b

Browse files
committed
fix(Compiler): failing tests
1 parent 00550cc commit 08f322b

File tree

6 files changed

+48
-38
lines changed

6 files changed

+48
-38
lines changed

src/Compiler/Module/Compiled/Remote.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,23 @@ private void UpdateArchiveContents() {
211211
Logger.Debug($"Updating archive contents for {this.ModuleSpec.Name}.");
212212

213213
var originalArchive = this.GetZipArchive();
214-
var expandedPath = Path.Join(RewritingFolder, this.ModuleSpec.Name);
214+
var uniqueModuleName = $"{this.ModuleSpec.Name}_{Guid.NewGuid():N}";
215+
216+
var expandedPath = Path.Join(RewritingFolder, uniqueModuleName);
215217
if (Directory.Exists(expandedPath)) Directory.Delete(expandedPath, true);
216218
Directory.CreateDirectory(expandedPath);
217219
originalArchive.ExtractToDirectory(expandedPath, true);
218220

219221
this.RewriteRequiredModules(expandedPath);
220222
this.MoveModuleManifest(expandedPath);
221223

222-
var tempArchivePath = Path.Join(RewritingFolder, $"{this.ModuleSpec.Name}.nupkg");
224+
var tempArchivePath = Path.Join(RewritingFolder, $"{uniqueModuleName}.nupkg");
223225
if (File.Exists(tempArchivePath)) File.Delete(tempArchivePath);
224226
ZipFile.CreateFromDirectory(expandedPath, tempArchivePath);
225227

226228
this.UpdatedContentBytes = File.ReadAllBytes(tempArchivePath);
227-
Logger.Debug($"Updated archive contents for {this.ModuleSpec.Name} and stored in {tempArchivePath}.");
229+
Directory.Delete(expandedPath, true);
230+
File.Delete(tempArchivePath);
228231
}
229232
}
230233

src/Compiler/Module/Resolvable/Remote.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ResolvableRemoteModule(ModuleSpec moduleSpec) : Resolvable(moduleSp
1717
private byte[]? Bytes;
1818

1919
// Only public for testing purposes.
20+
// Left value is a resolved file path, Right value is a task for creating that file
2021
public Atom<Either<Option<string>, Task<Option<string>>>>? CachedFile;
2122

2223
public string CachePath => Path.Join(

tests/Compiler/Analyser/Suppression.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ function Test-Function {
125125
[SuppressAnalyserAttribute('UseOfUndefinedFunction', 'That-Function', Justification = 'That Justification')]
126126
[Compiler.Analyser.SuppressAnalyser('UseOfUndefinedFunction', 'Other-Function', Justification = 'Other Justification')]
127127
[Compiler.Analyser.SuppressAnalyserAttribute('UseOfUndefinedFunction', 'Another-Function', Justification = 'Another Justification')]
128-
[SuppressAnalyser('UseOfUndefinedFunction', 'Other-Function')]
129-
[SuppressAnalyserAttribute('UseOfUndefinedFunction', 'Another-Function')]
130-
[Compiler.Analyser.SuppressAnalyser('UseOfUndefinedFunction', 'This-Function')]
131-
[Compiler.Analyser.SuppressAnalyserAttribute('UseOfUndefinedFunction', 'That-Function')]
132128
param()
133129
}
134130
""",
@@ -137,10 +133,6 @@ function Test-Function {
137133
new(typeof(UseOfUndefinedFunction), "That-Function", "That Justification"),
138134
new(typeof(UseOfUndefinedFunction), "Other-Function", "Other Justification"),
139135
new(typeof(UseOfUndefinedFunction), "Another-Function", "Another Justification"),
140-
new(typeof(UseOfUndefinedFunction), "This-Function", null),
141-
new(typeof(UseOfUndefinedFunction), "That-Function", null),
142-
new(typeof(UseOfUndefinedFunction), "Other-Function", null),
143-
new(typeof(UseOfUndefinedFunction), "Another-Function", null)
144136
});
145137
}
146138
}

tests/Compiler/Module/Compiled/Compiled.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ public static IEnumerable AddRequirementHashData {
9999
}
100100

101101
public static Mock<RealCompiled> GetMockCompiledModule(byte[]? bytes = null, CompiledScript? parent = null) {
102-
parent ??= CompiledLocalModuleTests.TestData.CreateModule<CompiledScript>();
103-
104102
var random = TestContext.CurrentContext.Random;
105103
var moduleSpec = new ModuleSpec(random.GetString(6));
106104
var requirements = new RequirementGroup();
@@ -112,13 +110,21 @@ public static Mock<RealCompiled> GetMockCompiledModule(byte[]? bytes = null, Com
112110
var mock = new Mock<RealCompiled>(moduleSpec, requirements, new Lazy<byte[]>(bytes)) {
113111
CallBase = true
114112
};
115-
CompiledUtils.AddDependency(parent, mock.Object);
113+
CompiledUtils.EnsureMockHasParent(mock.Object, parent);
116114

117115
return mock;
118116
}
119117
}
120118

121119
public static class CompiledUtils {
120+
public static CompiledScript EnsureMockHasParent(RealCompiled mock, CompiledScript? parent = null) {
121+
if (mock.Parents.Count != 0) return mock.GetRootParent()!;
122+
123+
parent ??= CompiledLocalModuleTests.TestData.CreateModule<CompiledScript>();
124+
AddDependency(parent, mock);
125+
return parent;
126+
}
127+
122128
public static void AddDependency(RealCompiled parent, RealCompiled dependency) {
123129
var rootParent = parent.GetRootParent()!;
124130

tests/Compiler/Module/Compiled/Local.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the GPL3 License, See LICENSE in the project root for license information.
33

44
using System.Management.Automation.Language;
5+
using System.Threading.Tasks;
56
using Compiler.Module.Compiled;
67
using Compiler.Requirements;
78
using Compiler.Text;
@@ -13,8 +14,8 @@ namespace Compiler.Test.Module.Compiled;
1314
[TestFixture]
1415
public class CompiledLocalModuleTests {
1516
[Test, Repeat(10), Parallelizable]
16-
public void StringifyContent_ReturnsValidAstContent() {
17-
var module = TestData.GetRandomCompiledModule();
17+
public async Task StringifyContent_ReturnsValidAstContent() {
18+
var module = await TestData.GetRandomCompiledModule();
1819
var stringifiedContent = module.StringifyContent();
1920
Assert.Multiple(() => {
2021
var ast = Parser.ParseInput(stringifiedContent, out _, out var errors);
@@ -24,10 +25,10 @@ public void StringifyContent_ReturnsValidAstContent() {
2425
}
2526

2627
[Test, Parallelizable]
27-
public void HashChanges(
28+
public async Task HashChanges(
2829
[Values("Hello, World!")] string scriptOneHello,
2930
[Values("Hello, World!", "Hello, Other World!")] string scriptTwoHello
30-
) => Assert.Multiple(() => {
31+
) => await Assert.MultipleAsync(async () => {
3132
var scriptOne = TestData.CreateModule<CompiledScript>($"Write-Host '{scriptOneHello}';");
3233
var scriptTwo = TestData.CreateModule<CompiledScript>($"Write-Host '{scriptTwoHello}';");
3334

@@ -37,7 +38,7 @@ public void HashChanges(
3738
if (scriptOneHello == scriptTwoHello) {
3839
var oldHash = scriptOne.ComputedHash;
3940

40-
var remoteModule = CompiledRemoteModuleTests.TestData.GetTestRemoteModule();
41+
var remoteModule = await CompiledRemoteModuleTests.TestData.GetTestRemoteModule();
4142
CompiledUtils.AddDependency(scriptOne, remoteModule);
4243
Assert.That(scriptOne.ComputedHash, Is.Not.EqualTo(oldHash), "Hash should change when a dependency is added.");
4344
Assert.That(scriptOne.ComputedHash, Is.Not.EqualTo(scriptTwo.ComputedHash), "Hashes should differ when a dependency is added.");
@@ -87,7 +88,7 @@ public static T CreateModule<T>(string? contents = null, string? fileNameNoExt =
8788
}.Object;
8889
}
8990

90-
public static RealCompiled GetRandomCompiledModule(CompiledLocalModule? parent = null, int depLevel = 0, bool createDependencies = true) {
91+
public static async Task<RealCompiled> GetRandomCompiledModule(CompiledLocalModule? parent = null, int depLevel = 0, bool createDependencies = true) {
9192
var random = TestContext.CurrentContext.Random;
9293
createDependencies = !createDependencies && depLevel < 3 && random.NextBool();
9394
var scriptParent = parent as CompiledScript ?? parent?.GetRootParent();
@@ -100,7 +101,7 @@ public static RealCompiled GetRandomCompiledModule(CompiledLocalModule? parent =
100101

101102
if (createDependencies) {
102103
for (var i = 0; i < random.Next(1, 5); i++) {
103-
var dependency = GetRandomCompiledModule(compiledScript, depLevel + 1, createDependencies);
104+
var dependency = await GetRandomCompiledModule(compiledScript, depLevel + 1, createDependencies);
104105
CompiledUtils.AddDependency(compiledScript, dependency);
105106
}
106107
}
@@ -112,15 +113,15 @@ public static RealCompiled GetRandomCompiledModule(CompiledLocalModule? parent =
112113

113114
if (createDependencies) {
114115
for (var i = 0; i < random.Next(1, 5); i++) {
115-
var dependency = GetRandomCompiledModule(module, depLevel + 1, createDependencies);
116+
var dependency = await GetRandomCompiledModule(module, depLevel + 1, createDependencies);
116117
CompiledUtils.AddDependency(module, dependency);
117118
}
118119
}
119120

120121
return module;
121122
}
122123
} else {
123-
var remoteModule = CompiledRemoteModuleTests.TestData.GetTestRemoteModule();
124+
var remoteModule = await CompiledRemoteModuleTests.TestData.GetTestRemoteModule();
124125
CompiledUtils.AddDependency(scriptParent!, remoteModule);
125126

126127
return remoteModule;

tests/Compiler/Module/Compiled/Remote.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
using System.Management.Automation.Language;
66
using System.Reflection;
77
using Compiler.Module.Compiled;
8+
using Compiler.Module.Resolvable;
89
using Compiler.Requirements;
9-
using Moq;
10+
using LanguageExt;
1011

1112
namespace Compiler.Test.Module.Compiled;
1213

1314
[TestFixture]
1415
public class CompiledRemoteModuleTests {
16+
public static Lock WritingResourceLock = new();
17+
1518
[Test, Repeat(10), Parallelizable]
16-
public void StringifyContent_ReturnsValidAst() {
17-
var module = TestData.GetTestRemoteModule();
19+
public async Task StringifyContent_ReturnsValidAst() {
20+
var module = await TestData.GetTestRemoteModule();
1821
var stringifiedContent = module.StringifyContent();
1922
Assert.Multiple(() => {
2023
var ast = Parser.ParseInput(stringifiedContent, out _, out var errors);
@@ -24,15 +27,13 @@ public void StringifyContent_ReturnsValidAst() {
2427
}
2528

2629
[Test, Repeat(10), Parallelizable]
27-
public void StringifyContent_CanBeConvertedBack() {
28-
// Convert the base64 string back to a byte array, then into a memory stream, then into a zip archive.
29-
var module = TestData.GetTestRemoteModule();
30+
public async Task StringifyContent_CanBeConvertedBack() {
31+
var module = await TestData.GetTestRemoteModule();
3032
var stringifiedContent = module.StringifyContent();
3133
var bytes = Convert.FromBase64String(stringifiedContent[1..^1]);
3234

3335
Assert.Multiple(() => {
3436
Assert.That(bytes, Is.Not.Empty);
35-
Assert.That(bytes, Is.EqualTo(module.ContentBytes.Value));
3637

3738
using var zipArchive = new ZipArchive(new MemoryStream(module.ContentBytes.Value), ZipArchiveMode.Read, false);
3839
Assert.That(zipArchive, Is.Not.Null);
@@ -49,22 +50,28 @@ public static class TestData {
4950
["PSReadLine"] = "2.3.5"
5051
};
5152

52-
public static CompiledRemoteModule GetTestRemoteModule() {
53+
public static async Task<CompiledRemoteModule> GetTestRemoteModule() {
5354
var random = TestContext.CurrentContext.Random;
5455
var (moduleName, moduleVersion) = TestableRemoteModules.ElementAt(random.Next(0, TestableRemoteModules.Count));
5556
var moduleSpec = new ModuleSpec(moduleName, requiredVersion: new Version(moduleVersion));
56-
var requirementGroup = new RequirementGroup();
57+
var parent = new ResolvableParent(TestContext.CurrentContext.TestDirectory);
58+
var resolvable = new ResolvableRemoteModule(moduleSpec);
5759

5860
var info = Assembly.GetExecutingAssembly().GetName();
5961
using var nupkgStream = Assembly.GetExecutingAssembly().GetManifestResourceStream($"{info.Name}.Resources.{moduleName}.{moduleVersion}.nupkg")!;
60-
var bytes = new byte[nupkgStream.Length];
61-
nupkgStream.ReadExactly(bytes);
62+
var tmpDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
63+
var tmpFile = Path.Combine(tmpDir, $"{moduleName}.{moduleVersion}.nupkg");
64+
lock (WritingResourceLock) {
65+
if (!Directory.Exists(tmpDir)) Directory.CreateDirectory(tmpDir);
6266

63-
var mock = new Mock<CompiledRemoteModule>(moduleSpec, requirementGroup, bytes) {
64-
CallBase = true
65-
};
67+
using var fileStream = new FileStream(tmpFile, FileMode.CreateNew, FileAccess.Write);
68+
nupkgStream.CopyTo(fileStream);
69+
}
70+
resolvable.CachedFile = Prelude.Atom(Either<Option<string>, Task<Option<string>>>.Left(tmpFile.AsOption()));
6671

67-
return mock.Object;
72+
var module = (await resolvable.IntoCompiled(parent)).Unwrap() as CompiledRemoteModule;
73+
CompiledUtils.EnsureMockHasParent(module!);
74+
return module!;
6875
}
6976
}
7077
}

0 commit comments

Comments
 (0)