private (FilePath, LocalToolsCommandResolver) DefaultSetup(string toolCommand) { NuGetVersion packageVersionA = NuGetVersion.Parse("1.0.4"); _fileSystem.File.WriteAllText(Path.Combine(_testDirectoryRoot, ManifestFilename), _jsonContent.Replace("$TOOLCOMMAND$", toolCommand)); ToolManifestFinder toolManifest = new ToolManifestFinder(new DirectoryPath(_testDirectoryRoot), _fileSystem, new FakeDangerousFileDetector()); ToolCommandName toolCommandNameA = new ToolCommandName(toolCommand); FilePath fakeExecutable = _nugetGlobalPackagesFolder.WithFile("fakeExecutable.dll"); _fileSystem.Directory.CreateDirectory(_nugetGlobalPackagesFolder.Value); _fileSystem.File.CreateEmptyFile(fakeExecutable.Value); _localToolsResolverCache.Save( new Dictionary <RestoredCommandIdentifier, RestoredCommand> { [new RestoredCommandIdentifier( new PackageId("local.tool.console.a"), packageVersionA, NuGetFramework.Parse(BundledTargetFramework.GetTargetFrameworkMoniker()), Constants.AnyRid, toolCommandNameA)] = new RestoredCommand(toolCommandNameA, "dotnet", fakeExecutable) }); var localToolsCommandResolver = new LocalToolsCommandResolver( toolManifest, _localToolsResolverCache, _fileSystem); return(fakeExecutable, localToolsCommandResolver); }
public void WhenTheCacheIsCorruptedItShouldNotAffectNextSaveAndLoad() { IFileSystem fileSystem = new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build(); DirectoryPath tempDirectory = new DirectoryPath(fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath); DirectoryPath cacheDirectory = tempDirectory.WithSubDirectories("cacheDirectory"); DirectoryPath nuGetGlobalPackagesFolder = tempDirectory.WithSubDirectories("nugetGlobalPackageLocation"); fileSystem.Directory.CreateDirectory(cacheDirectory.Value); const int version = 1; LocalToolsResolverCache localToolsResolverCache = new LocalToolsResolverCache(fileSystem, cacheDirectory, version); NuGetFramework targetFramework = NuGetFramework.Parse("netcoreapp2.1"); string runtimeIdentifier = Constants.AnyRid; PackageId packageId = new PackageId("my.toolBundle"); NuGetVersion nuGetVersion = NuGetVersion.Parse("1.0.2"); IReadOnlyList <RestoredCommand> restoredCommands = new[] { new RestoredCommand(new ToolCommandName("tool1"), "dotnet", nuGetGlobalPackagesFolder.WithFile("tool1.dll")), }; localToolsResolverCache.Save( restoredCommands.ToDictionary( c => new RestoredCommandIdentifier(packageId, nuGetVersion, targetFramework, runtimeIdentifier, c.Name)), nuGetGlobalPackagesFolder); var cachePath = cacheDirectory .WithSubDirectories(version.ToString()) .WithSubDirectories(packageId.ToString()).Value; var existingCache = fileSystem.File.ReadAllText( cachePath); existingCache.Should().NotBeEmpty(); fileSystem.File.WriteAllText(cachePath, existingCache + " !!!Corrupted"); // Save after corruption localToolsResolverCache.Save( restoredCommands.ToDictionary( c => new RestoredCommandIdentifier(packageId, nuGetVersion, targetFramework, runtimeIdentifier, c.Name)), nuGetGlobalPackagesFolder); localToolsResolverCache.TryLoad( new RestoredCommandIdentifier(packageId, nuGetVersion, targetFramework, runtimeIdentifier, restoredCommands[0].Name), nuGetGlobalPackagesFolder, out RestoredCommand restoredCommand); restoredCommand.ShouldBeEquivalentTo(restoredCommands[0]); }
public void ItCanFindToolExecutable() { var toolCommand = "a"; NuGetVersion packageVersionA = NuGetVersion.Parse("1.0.4"); _fileSystem.File.WriteAllText(Path.Combine(_testDirectoryRoot, ManifestFilename), _jsonContent.Replace("$TOOLCOMMAND$", toolCommand)); ToolManifestFinder toolManifest = new ToolManifestFinder(new DirectoryPath(_testDirectoryRoot), _fileSystem); ToolCommandName toolCommandNameA = new ToolCommandName(toolCommand); var fakeExecutable = _nugetGlobalPackagesFolder.WithFile("fakeExecutable.dll"); _fileSystem.Directory.CreateDirectory(_nugetGlobalPackagesFolder.Value); _fileSystem.File.CreateEmptyFile(fakeExecutable.Value); _localToolsResolverCache.Save( new Dictionary <RestoredCommandIdentifier, RestoredCommand> { [new RestoredCommandIdentifier( new PackageId("local.tool.console.a"), packageVersionA, NuGetFramework.Parse(BundledTargetFramework.GetTargetFrameworkMoniker()), Constants.AnyRid, toolCommandNameA)] = new RestoredCommand(toolCommandNameA, "dotnet", fakeExecutable) }, _nugetGlobalPackagesFolder); var localToolsCommandResolver = new LocalToolsCommandResolver( toolManifest, _localToolsResolverCache, _fileSystem, _nugetGlobalPackagesFolder); var result = localToolsCommandResolver.Resolve(new CommandResolverArguments() { CommandName = "dotnet-a", }); result.Should().NotBeNull(); var commandPath = result.Args.Trim('"'); _fileSystem.File.Exists(commandPath).Should().BeTrue("the following path exists: " + commandPath); commandPath.Should().Be(fakeExecutable.Value); }
private static void WhenTheCacheIsCorruptedItShouldLoadAsEmpty( bool useRealFileSystem, Action <IFileSystem, string, string> corruptCache) { IFileSystem fileSystem = useRealFileSystem == false ? new FileSystemMockBuilder().UseCurrentSystemTemporaryDirectory().Build() : new FileSystemWrapper(); DirectoryPath tempDirectory = new DirectoryPath(fileSystem.Directory.CreateTemporaryDirectory().DirectoryPath); DirectoryPath cacheDirectory = tempDirectory.WithSubDirectories("cacheDirectory"); DirectoryPath nuGetGlobalPackagesFolder = tempDirectory.WithSubDirectories("nugetGlobalPackageLocation"); fileSystem.Directory.CreateDirectory(cacheDirectory.Value); const int version = 1; LocalToolsResolverCache localToolsResolverCache = new LocalToolsResolverCache(fileSystem, cacheDirectory, version); NuGetFramework targetFramework = NuGetFramework.Parse("netcoreapp2.1"); string runtimeIdentifier = Constants.AnyRid; PackageId packageId = new PackageId("my.toolBundle"); NuGetVersion nuGetVersion = NuGetVersion.Parse("1.0.2"); IReadOnlyList <RestoredCommand> restoredCommands = new[] { new RestoredCommand(new ToolCommandName("tool1"), "dotnet", nuGetGlobalPackagesFolder.WithFile("tool1.dll")), }; localToolsResolverCache.Save( restoredCommands.ToDictionary( c => new RestoredCommandIdentifier(packageId, nuGetVersion, targetFramework, runtimeIdentifier, c.Name)), nuGetGlobalPackagesFolder); var cachePath = cacheDirectory .WithSubDirectories(version.ToString()) .WithSubDirectories(packageId.ToString()).Value; var existingCache = fileSystem.File.ReadAllText( cachePath); existingCache.Should().NotBeEmpty(); corruptCache(fileSystem, cachePath, existingCache); localToolsResolverCache.TryLoad( new RestoredCommandIdentifier(packageId, nuGetVersion, targetFramework, runtimeIdentifier, restoredCommands[0].Name), nuGetGlobalPackagesFolder, out _).Should().BeFalse("Consider corrupted file cache miss"); }