|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 7 | +using Microsoft.Web.LibraryManager.Contracts; |
| 8 | +using Microsoft.Web.LibraryManager.Json; |
| 9 | +using Microsoft.Web.LibraryManager.LibraryNaming; |
| 10 | +using Microsoft.Web.LibraryManager.Mocks; |
| 11 | +using Microsoft.Web.LibraryManager.Providers.Cdnjs; |
| 12 | + |
| 13 | +namespace Microsoft.Web.LibraryManager.Test.Json |
| 14 | +{ |
| 15 | + [TestClass] |
| 16 | + public class LibraryStateToFileConverterTests |
| 17 | + { |
| 18 | + [TestInitialize] |
| 19 | + public void Setup() |
| 20 | + { |
| 21 | + string cacheFolder = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\"); |
| 22 | + string projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager"); |
| 23 | + var hostInteraction = new HostInteraction(projectFolder, cacheFolder); |
| 24 | + var dependencies = new Dependencies(hostInteraction, new CdnjsProviderFactory()); |
| 25 | + IProvider provider = dependencies.GetProvider("cdnjs"); |
| 26 | + LibraryIdToNameAndVersionConverter.Instance.Reinitialize(dependencies); |
| 27 | + } |
| 28 | + |
| 29 | + [TestMethod] |
| 30 | + public void ConvertToLibraryInstallationState_NullStateOnDisk() |
| 31 | + { |
| 32 | + LibraryStateToFileConverter converter = new LibraryStateToFileConverter("provider", "destination"); |
| 33 | + |
| 34 | + ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(null); |
| 35 | + |
| 36 | + Assert.IsNull(result); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void ConvertToLibraryInstallationState_UseDefaultProviderAndDestination() |
| 41 | + { |
| 42 | + LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "defaultDestination"); |
| 43 | + |
| 44 | + var stateOnDisk = new LibraryInstallationStateOnDisk |
| 45 | + { |
| 46 | + LibraryId = "libraryId", |
| 47 | + }; |
| 48 | + |
| 49 | + ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); |
| 50 | + |
| 51 | + Assert.AreEqual("defaultProvider", result.ProviderId); |
| 52 | + Assert.AreEqual("defaultDestination", result.DestinationPath); |
| 53 | + } |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void ConvertToLibraryInstallationState_OverrideProviderAndDestination() |
| 57 | + { |
| 58 | + LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "defaultDestination"); |
| 59 | + |
| 60 | + var stateOnDisk = new LibraryInstallationStateOnDisk |
| 61 | + { |
| 62 | + LibraryId = "libraryId", |
| 63 | + ProviderId = "provider", |
| 64 | + DestinationPath = "destination", |
| 65 | + }; |
| 66 | + |
| 67 | + ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); |
| 68 | + |
| 69 | + Assert.AreEqual("provider", result.ProviderId); |
| 70 | + Assert.AreEqual("destination", result.DestinationPath); |
| 71 | + } |
| 72 | + |
| 73 | + [TestMethod] |
| 74 | + public void ConvertToLibraryInstallationState_ExpandTokensInDefaultDestination() |
| 75 | + { |
| 76 | + LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "lib/[Name]/[Version]"); |
| 77 | + |
| 78 | + var stateOnDisk = new LibraryInstallationStateOnDisk |
| 79 | + { |
| 80 | + LibraryId = "testLibraryId@1.0", |
| 81 | + // it needs to be a provider that uses the versioned naming scheme |
| 82 | + ProviderId = "cdnjs", |
| 83 | + }; |
| 84 | + |
| 85 | + ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); |
| 86 | + |
| 87 | + Assert.AreEqual("lib/testLibraryId/1.0", result.DestinationPath); |
| 88 | + } |
| 89 | + |
| 90 | + [TestMethod] |
| 91 | + [DataRow("filesystem", "c:\\path\\to\\library")] |
| 92 | + [DataRow("filesystem", "/path/to/library")] |
| 93 | + [DataRow("cdnjs", "@scope/library@1.0.0")] |
| 94 | + public void ConvertToLibraryInstallationState_ExpandTokensInDefaultDestination_NamesWithSlashes(string provider, string libraryId) |
| 95 | + { |
| 96 | + LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "lib/[Name]"); |
| 97 | + |
| 98 | + var stateOnDisk = new LibraryInstallationStateOnDisk |
| 99 | + { |
| 100 | + LibraryId = libraryId, |
| 101 | + ProviderId = provider, |
| 102 | + }; |
| 103 | + |
| 104 | + ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); |
| 105 | + |
| 106 | + Assert.AreEqual("lib/library", result.DestinationPath); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments