Skip to content

Commit

Permalink
Refactor workload garbage collection tests, add test for side-by-side…
Browse files Browse the repository at this point in the history
… manifest collection
  • Loading branch information
dsplaisted committed Sep 29, 2023
1 parent 288c9a3 commit 9061e0a
Show file tree
Hide file tree
Showing 5 changed files with 486 additions and 100 deletions.
4 changes: 2 additions & 2 deletions src/Assets/TestProjects/SampleManifest/Sample2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "5.0.0-preview1",
"version": "1.0.0",
"workloads": {
"xamarin-android": {
"description": "Create, build and run Android apps",
Expand Down Expand Up @@ -58,7 +58,7 @@
"version": "8.4.0"
},
"Xamarin.Android.Runtime": {
"kind": "framework",
"kind": "library",
"version": "8.4.7.4"
},
"Xamarin.Android.BuildTools": {
Expand Down
82 changes: 82 additions & 0 deletions src/Assets/TestProjects/SampleManifest/Sample2_v2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"version": "2.0.0",
"workloads": {
"xamarin-android": {
"description": "Create, build and run Android apps",
"kind": "dev",
"packs": [
"Xamarin.Android.Templates"
],
"extends": [
"xamarin-android-build"
]
},
"xamarin-android-build": {
"description": "Build and run Android apps",
"packs": [
"Xamarin.Android.Sdk",
"Xamarin.Android.Framework",
"Xamarin.Android.Runtime"
]
},
"android-sdk-workload":{
"description": "Test workload",
"packs": [
"Xamarin.Android.Sdk"
]
},
"android-templates-workload":{
"description": "Test workload",
"packs": [
"Xamarin.Android.Templates"
]
},
"android-buildtools-workload":{
"description": "Test workload",
"packs": [
"Xamarin.Android.BuildTools"
]
},
"xamarin-empty-mock": {
"description": "Empty mock workload for testing",
"kind": "dev",
"packs": [],
"extends": []
}
},
"packs": {
"Xamarin.Android.Sdk": {
"kind": "sdk",
"version": "8.4.7"
},
"Xamarin.Android.Templates": {
"kind": "template",
"version": "1.1.0"
},
"Xamarin.Android.Framework": {
"kind": "framework",
"version": "8.5.0"
},
"Xamarin.Android.Runtime": {
"kind": "library",
"version": "8.5.0.1"
},
"Xamarin.Android.BuildTools": {
"version": "8.5.0",
"kind": "sdk",
"alias-to": {
"osx": "Xamarin.Android.BuildTools.MacHost",
"win": "Xamarin.Android.BuildTools.WinHost",
"linux": "Xamarin.Android.BuildTools.LinuxHost"
}
},
"Test.Pack.A": {
"version": "1.0.0",
"kind": "sdk"
},
"Test.Pack.B": {
"version": "2.0.0",
"kind": "framework"
}
}
}
82 changes: 82 additions & 0 deletions src/Assets/TestProjects/SampleManifest/Sample2_v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"version": "3.0.0",
"workloads": {
"xamarin-android": {
"description": "Create, build and run Android apps",
"kind": "dev",
"packs": [
"Xamarin.Android.Templates"
],
"extends": [
"xamarin-android-build"
]
},
"xamarin-android-build": {
"description": "Build and run Android apps",
"packs": [
"Xamarin.Android.Sdk",
"Xamarin.Android.Framework",
"Xamarin.Android.Runtime"
]
},
"android-sdk-workload":{
"description": "Test workload",
"packs": [
"Xamarin.Android.Sdk"
]
},
"android-templates-workload":{
"description": "Test workload",
"packs": [
"Xamarin.Android.Templates"
]
},
"android-buildtools-workload":{
"description": "Test workload",
"packs": [
"Xamarin.Android.BuildTools"
]
},
"xamarin-empty-mock": {
"description": "Empty mock workload for testing",
"kind": "dev",
"packs": [],
"extends": []
}
},
"packs": {
"Xamarin.Android.Sdk": {
"kind": "sdk",
"version": "8.4.7"
},
"Xamarin.Android.Templates": {
"kind": "template",
"version": "1.2.0"
},
"Xamarin.Android.Framework": {
"kind": "framework",
"version": "8.6.0"
},
"Xamarin.Android.Runtime": {
"kind": "library",
"version": "8.6.0.0"
},
"Xamarin.Android.BuildTools": {
"version": "8.6.0",
"kind": "sdk",
"alias-to": {
"osx": "Xamarin.Android.BuildTools.MacHost",
"win": "Xamarin.Android.BuildTools.WinHost",
"linux": "Xamarin.Android.BuildTools.LinuxHost"
}
},
"Test.Pack.A": {
"version": "1.0.0",
"kind": "sdk"
},
"Test.Pack.B": {
"version": "2.0.0",
"kind": "framework"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,101 +220,6 @@ public void GivenManagedInstallItCanRollBackInstallFailures()
Directory.Exists(Path.Combine(dotnetRoot, "packs", packId, packVersion)).Should().BeFalse();
}

[Fact]
public void GivenManagedInstallItCanGarbageCollect()
{
var (dotnetRoot, installer, _, getResolver) = GetTestInstaller();
var packs = new PackInfo[]
{
CreatePackInfo("Xamarin.Android.Sdk", "8.4.7", WorkloadPackKind.Library, Path.Combine(dotnetRoot, "library-packs", "Xamarin.Android.Sdk.8.4.7.nupkg"), "Xamarin.Android.Sdk"),
CreatePackInfo("Xamarin.Android.Framework", "8.4.0", WorkloadPackKind.Framework, Path.Combine(dotnetRoot, "packs", "Xamarin.Android.Framework", "8.4.0"), "Xamarin.Android.Framework")
};
var sdkVersions = new WorkloadId[] { new WorkloadId("6.0.100"), new WorkloadId("6.0.300") };

// Write fake packs
var installedPacksPath = Path.Combine(dotnetRoot, "metadata", "workloads", "InstalledPacks", "v1");
foreach (var sdkVersion in sdkVersions)
{
foreach (var pack in packs)
{
var packRecordPath = Path.Combine(installedPacksPath, pack.Id, pack.Version, sdkVersion);
Directory.CreateDirectory(Path.GetDirectoryName(packRecordPath));
var packRecordContents = JsonSerializer.Serialize<WorkloadResolver.PackInfo>(pack);
File.WriteAllText(packRecordPath, packRecordContents);
if (pack.Kind == WorkloadPackKind.Library)
{
Directory.CreateDirectory(Path.GetDirectoryName(pack.Path));
using var _ = File.Create(pack.Path);
}
else
{
Directory.CreateDirectory(pack.Path);
}
}
}
// Write fake install record for 6.0.300
var workloadsRecordPath = Path.Combine(dotnetRoot, "metadata", "workloads", sdkVersions[1], "InstalledWorkloads");
Directory.CreateDirectory(workloadsRecordPath);
File.Create(Path.Combine(workloadsRecordPath, "xamarin-empty-mock"));

installer.GarbageCollect(getResolver);

Directory.EnumerateFileSystemEntries(installedPacksPath)
.Should()
.BeEmpty();
foreach (var pack in packs)
{
if (pack.Kind == WorkloadPackKind.Library)
{
File.Exists(pack.Path).Should().BeFalse();
}
else
{
Directory.Exists(pack.Path)
.Should()
.BeFalse();
}
}
}

[Fact]
public void GivenManagedInstallItCanGarbageCollectPacksMissingFromManifest()
{
var (dotnetRoot, installer, _, getResolver) = GetTestInstaller();
// Define packs that don't show up in the manifest
var packs = new PackInfo[]
{
CreatePackInfo("Xamarin.Android.Sdk.fake", "8.4.7", WorkloadPackKind.Framework, Path.Combine(dotnetRoot, "packs", "Xamarin.Android.Sdk.fake", "8.4.7"), "Xamarin.Android.Sdk.fake"),
CreatePackInfo("Xamarin.Android.Framework.mock", "8.4", WorkloadPackKind.Framework, Path.Combine(dotnetRoot, "packs", "Xamarin.Android.Framework.mock", "8.4"), "Xamarin.Android.Framework.mock")
};
var sdkVersions = new WorkloadId[] { new WorkloadId("6.0.100"), new WorkloadId("6.0.300") };

// Write fake packs
var installedPacksPath = Path.Combine(dotnetRoot, "metadata", "workloads", "InstalledPacks", "v1");
foreach (var sdkVersion in sdkVersions)
{
foreach (var pack in packs)
{
var packRecordPath = Path.Combine(installedPacksPath, pack.Id, pack.Version, sdkVersion);
Directory.CreateDirectory(Path.GetDirectoryName(packRecordPath));
File.WriteAllText(packRecordPath, JsonSerializer.Serialize(pack));
Directory.CreateDirectory(pack.Path);
}
}

installer.GarbageCollect(getResolver);

Directory.EnumerateFileSystemEntries(installedPacksPath)
.Should()
.BeEmpty();
foreach (var pack in packs)
{
Directory.Exists(pack.Path)
.Should()
.BeFalse();
}
}

[Fact]
public void GivenManagedInstallItDoesNotRemovePacksWithInstallRecords()
{
Expand Down Expand Up @@ -484,11 +389,11 @@ public void GivenManagedInstallItCanErrorsWhenMissingOfflineCache()
var workloadResolver = WorkloadResolver.CreateForTests(new MockManifestProvider(new[] { _manifestPath }), dotnetRoot);
var sdkFeatureBand = new SdkFeatureBand("6.0.300");

IWorkloadResolver GetResolver(string sdkVersion)
IWorkloadResolver GetResolver(string workloadSetVersion)
{
if (sdkVersion != null && !sdkFeatureBand.Equals(new SdkFeatureBand(sdkVersion)))
if (workloadSetVersion != null && !sdkFeatureBand.Equals(new SdkFeatureBand(workloadSetVersion)))
{
throw new NotSupportedException("Mock doesn't support creating resolver for different feature bands: " + sdkVersion);
throw new NotSupportedException("Mock doesn't support creating resolver for different feature bands: " + workloadSetVersion);
}
return workloadResolver;
}
Expand Down
Loading

0 comments on commit 9061e0a

Please sign in to comment.