Skip to content
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 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/LibraryManager/Json/LibraryStateToFileConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.Web.LibraryManager.Contracts;
using Microsoft.Web.LibraryManager.LibraryNaming;

Expand All @@ -25,22 +27,49 @@ public ILibraryInstallationState ConvertToLibraryInstallationState(LibraryInstal
}

string provider = string.IsNullOrEmpty(stateOnDisk.ProviderId) ? _defaultProvider : stateOnDisk.ProviderId;
string destination = string.IsNullOrEmpty(stateOnDisk.DestinationPath) ? _defaultDestination : stateOnDisk.DestinationPath;

(string name, string version) = LibraryIdToNameAndVersionConverter.Instance.GetLibraryNameAndVersion(stateOnDisk.LibraryId, provider);
string destination = string.IsNullOrEmpty(stateOnDisk.DestinationPath) ? ExpandDestination(_defaultDestination, name, version) : stateOnDisk.DestinationPath;

var state = new LibraryInstallationState()
{
Name = name,
Version = version,
IsUsingDefaultDestination = string.IsNullOrEmpty(stateOnDisk.DestinationPath),
IsUsingDefaultProvider = string.IsNullOrEmpty(stateOnDisk.ProviderId),
ProviderId = provider,
DestinationPath = destination,
Files = stateOnDisk.Files
};

(state.Name, state.Version) = LibraryIdToNameAndVersionConverter.Instance.GetLibraryNameAndVersion(stateOnDisk.LibraryId, provider);

return state;
}

/// <summary>
/// Expands [Name] and [Version] tokens in the DefaultDestination
/// </summary>
/// <param name="destination">The default destination string</param>
/// <param name="name">Package name</param>
/// <param name="version">Package version</param>
/// <returns></returns>
[SuppressMessage("Globalization", "CA1307:Specify StringComparison for clarity", Justification = "Not available on net481, not needed here (caseless)")]
private string ExpandDestination(string destination, string name, string version)
{
if (!destination.Contains("["))
{
return destination;
}

// if the name contains a slash (either filesystem or scoped packages),
// trim that and only take the last segment.
int cutIndex = name.LastIndexOfAny(['/', '\\']);

StringBuilder stringBuilder = new StringBuilder(destination);
stringBuilder.Replace("[Name]", cutIndex == -1 ? name : name.Substring(cutIndex + 1));
Copy link
Contributor

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.

Copy link
Contributor Author

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.

stringBuilder.Replace("[Version]", version);
return stringBuilder.ToString();
}

public LibraryInstallationStateOnDisk ConvertToLibraryInstallationStateOnDisk(ILibraryInstallationState state)
{
if (state == null)
Expand Down
109 changes: 109 additions & 0 deletions test/LibraryManager.Test/Json/LibraryStateToFileConverterTests.cs
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

}
}