-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add variable expansion in defaultDestination #759
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
test/LibraryManager.Test/Json/LibraryStateToFileConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Microsoft.Web.LibraryManager.Contracts; | ||
using Microsoft.Web.LibraryManager.Json; | ||
using Microsoft.Web.LibraryManager.LibraryNaming; | ||
using Microsoft.Web.LibraryManager.Mocks; | ||
using Microsoft.Web.LibraryManager.Providers.Cdnjs; | ||
|
||
namespace Microsoft.Web.LibraryManager.Test.Json | ||
{ | ||
[TestClass] | ||
public class LibraryStateToFileConverterTests | ||
{ | ||
[TestInitialize] | ||
public void Setup() | ||
{ | ||
string cacheFolder = Environment.ExpandEnvironmentVariables(@"%localappdata%\Microsoft\Library\"); | ||
string projectFolder = Path.Combine(Path.GetTempPath(), "LibraryManager"); | ||
var hostInteraction = new HostInteraction(projectFolder, cacheFolder); | ||
var dependencies = new Dependencies(hostInteraction, new CdnjsProviderFactory()); | ||
IProvider provider = dependencies.GetProvider("cdnjs"); | ||
LibraryIdToNameAndVersionConverter.Instance.Reinitialize(dependencies); | ||
} | ||
|
||
[TestMethod] | ||
public void ConvertToLibraryInstallationState_NullStateOnDisk() | ||
{ | ||
LibraryStateToFileConverter converter = new LibraryStateToFileConverter("provider", "destination"); | ||
|
||
ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(null); | ||
|
||
Assert.IsNull(result); | ||
} | ||
|
||
[TestMethod] | ||
public void ConvertToLibraryInstallationState_UseDefaultProviderAndDestination() | ||
{ | ||
LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "defaultDestination"); | ||
|
||
var stateOnDisk = new LibraryInstallationStateOnDisk | ||
{ | ||
LibraryId = "libraryId", | ||
}; | ||
|
||
ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); | ||
|
||
Assert.AreEqual("defaultProvider", result.ProviderId); | ||
Assert.AreEqual("defaultDestination", result.DestinationPath); | ||
} | ||
|
||
[TestMethod] | ||
public void ConvertToLibraryInstallationState_OverrideProviderAndDestination() | ||
{ | ||
LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "defaultDestination"); | ||
|
||
var stateOnDisk = new LibraryInstallationStateOnDisk | ||
{ | ||
LibraryId = "libraryId", | ||
ProviderId = "provider", | ||
DestinationPath = "destination", | ||
}; | ||
|
||
ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); | ||
|
||
Assert.AreEqual("provider", result.ProviderId); | ||
Assert.AreEqual("destination", result.DestinationPath); | ||
} | ||
|
||
[TestMethod] | ||
public void ConvertToLibraryInstallationState_ExpandTokensInDefaultDestination() | ||
{ | ||
LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "lib/[Name]/[Version]"); | ||
|
||
var stateOnDisk = new LibraryInstallationStateOnDisk | ||
{ | ||
LibraryId = "[email protected]", | ||
// it needs to be a provider that uses the versioned naming scheme | ||
ProviderId = "cdnjs", | ||
}; | ||
|
||
ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); | ||
|
||
Assert.AreEqual("lib/testLibraryId/1.0", result.DestinationPath); | ||
} | ||
|
||
[TestMethod] | ||
[DataRow("filesystem", "c:\\path\\to\\library")] | ||
[DataRow("filesystem", "/path/to/library")] | ||
[DataRow("cdnjs", "@scope/[email protected]")] | ||
public void ConvertToLibraryInstallationState_ExpandTokensInDefaultDestination_NamesWithSlashes(string provider, string libraryId) | ||
{ | ||
LibraryStateToFileConverter converter = new LibraryStateToFileConverter("defaultProvider", "lib/[Name]"); | ||
|
||
var stateOnDisk = new LibraryInstallationStateOnDisk | ||
{ | ||
LibraryId = libraryId, | ||
ProviderId = provider, | ||
}; | ||
|
||
ILibraryInstallationState result = converter.ConvertToLibraryInstallationState(stateOnDisk); | ||
|
||
Assert.AreEqual("lib/library", result.DestinationPath); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great to see some explicit tests of ExpandDestination that showed the 'before' and 'after' as DataRows. |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possibly a non-real case, but what if name contains [Version]? That would cause the next line to do an extra/unexpected replacement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least as far as NPM is concerned, I don't believe that is a valid name (only allows alphanumeric, underscores, and hyphens). Since that's the source for the non-filesystem providers (jsdelivr and unpkg directly use NPM, cdnjs is indirectly curated from NPM), it would be an edge case for filesystem only. I thought about making this logic provider-specific, but that was a lot more complicated than doing it here centrally.
I did briefly consider using a different token that wouldn't be allowed in file paths (implicitly compatible with all providers since they all come down to file names), but on Linux the only character is
/
. I'm definitely open another delineator if you have a preference, but[Version]
felt most natural (next on my list was something like{{Version}}
); it should be a pretty trivial change.