-
Notifications
You must be signed in to change notification settings - Fork 375
NuGet package licenses #2003
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
Merged
Merged
NuGet package licenses #2003
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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
37 changes: 37 additions & 0 deletions
37
src/Microsoft.DotNet.Arcade.Sdk.Tests/GetLicenseFilePathTests.cs
This file contains hidden or 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,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using Xunit; | ||
|
||
namespace Microsoft.DotNet.Arcade.Sdk.Tests | ||
{ | ||
public class GetLicenseFilePathTests | ||
{ | ||
[Theory] | ||
[InlineData("licenSe.TXT")] | ||
[InlineData("license.md")] | ||
[InlineData("LICENSE")] | ||
public void GetLicenseFilePath(string licenseFileName) | ||
{ | ||
var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
Directory.CreateDirectory(dir); | ||
var licensePath = Path.Combine(dir, licenseFileName); | ||
|
||
File.WriteAllText(licensePath, ""); | ||
|
||
var task = new GetLicenseFilePath() | ||
{ | ||
Directory = dir | ||
}; | ||
|
||
bool result = task.Execute(); | ||
Assert.Equal(licensePath, task.Path); | ||
Assert.True(result); | ||
|
||
Directory.Delete(dir, recursive: true); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Microsoft.DotNet.Arcade.Sdk.Tests/ValidateLicenseTests.cs
This file contains hidden or 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,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Xunit; | ||
|
||
namespace Microsoft.DotNet.Arcade.Sdk.Tests | ||
{ | ||
public class ValidateLicenseTests | ||
{ | ||
[Fact] | ||
public void LinesEqual() | ||
{ | ||
Assert.False(ValidateLicense.LinesEqual(new[] { "a" }, new[] { "b" })); | ||
Assert.False(ValidateLicense.LinesEqual(new[] { "a" }, new[] { "A" })); | ||
Assert.False(ValidateLicense.LinesEqual(new[] { "a" }, new[] { "a", "b" })); | ||
Assert.False(ValidateLicense.LinesEqual(new[] { "a" }, new[] { "a", "*ignore-line*" })); | ||
Assert.False(ValidateLicense.LinesEqual(new[] { "*ignore-line*" }, new[] { "a" })); | ||
Assert.True(ValidateLicense.LinesEqual(new[] { "a" }, new[] { "*ignore-line*" })); | ||
|
||
Assert.True(ValidateLicense.LinesEqual(new[] { "a", " ", " b", "xxx", "\t \t" }, new[] { "a", "b ", "*ignore-line*" })); | ||
} | ||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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,76 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Microsoft.DotNet.Arcade.Sdk | ||
{ | ||
/// <summary> | ||
/// Finds a license file in the given directory. | ||
/// File is considered a license file if its name matches 'license(.txt|.md|)', ignoring case. | ||
/// </summary> | ||
public class GetLicenseFilePath : Task | ||
{ | ||
/// <summary> | ||
/// Full path to the directory to search for the license file. | ||
/// </summary> | ||
[Required] | ||
public string Directory { get; set; } | ||
|
||
/// <summary> | ||
/// Full path to the license file, or empty if it is not found. | ||
/// </summary> | ||
[Output] | ||
public string Path { get; private set; } | ||
|
||
public override bool Execute() | ||
{ | ||
ExecuteImpl(); | ||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
private void ExecuteImpl() | ||
{ | ||
const string fileName = "license"; | ||
|
||
#if NET472 | ||
IEnumerable<string> enumerateFiles(string extension) => | ||
System.IO.Directory.EnumerateFiles(Directory, fileName + extension, SearchOption.TopDirectoryOnly); | ||
#else | ||
var options = new EnumerationOptions | ||
{ | ||
MatchCasing = MatchCasing.CaseInsensitive, | ||
RecurseSubdirectories = false, | ||
MatchType = MatchType.Simple | ||
}; | ||
|
||
options.AttributesToSkip |= FileAttributes.Directory; | ||
|
||
IEnumerable<string> enumerateFiles(string extension) => | ||
System.IO.Directory.EnumerateFileSystemEntries(Directory, fileName + extension, options); | ||
#endif | ||
var matches = | ||
(from extension in new[] { ".txt", ".md", "" } | ||
from path in enumerateFiles(extension) | ||
select path).ToArray(); | ||
|
||
if (matches.Length == 0) | ||
{ | ||
Log.LogError($"No license file found in '{Directory}'."); | ||
} | ||
else if (matches.Length > 1) | ||
{ | ||
Log.LogError($"Multiple license files found in '{Directory}': '{string.Join("', '", matches)}'."); | ||
} | ||
else | ||
{ | ||
Path = matches[0]; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,80 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Microsoft.DotNet.Arcade.Sdk | ||
{ | ||
/// <summary> | ||
/// Checks that the content of two license files is the same modulo line breaks, leading and trailing whitespace. | ||
/// </summary> | ||
public class ValidateLicense : Task | ||
{ | ||
/// <summary> | ||
/// Full path to the file that contains the license text to be validated. | ||
/// </summary> | ||
[Required] | ||
public string LicensePath { get; set; } | ||
|
||
/// <summary> | ||
/// Full path to the file that contains expected license text. | ||
/// </summary> | ||
[Required] | ||
public string ExpectedLicensePath { get; set; } | ||
|
||
public override bool Execute() | ||
{ | ||
ExecuteImpl(); | ||
return !Log.HasLoggedErrors; | ||
} | ||
|
||
private void ExecuteImpl() | ||
{ | ||
var actualLines = File.ReadAllLines(LicensePath, Encoding.UTF8); | ||
var expectedLines = File.ReadAllLines(ExpectedLicensePath, Encoding.UTF8); | ||
|
||
if (!LinesEqual(actualLines, expectedLines)) | ||
{ | ||
Log.LogError($"License file content '{LicensePath}' doesn't match the expected license '{ExpectedLicensePath}'."); | ||
} | ||
} | ||
|
||
internal static bool LinesEqual(IEnumerable<string> actual, IEnumerable<string> expected) | ||
{ | ||
IEnumerable<string> normalize(IEnumerable<string> lines) | ||
=> from line in lines | ||
where !string.IsNullOrWhiteSpace(line) | ||
select line.Trim(); | ||
|
||
var normalizedActual = normalize(actual).ToArray(); | ||
var normalizedExpected = normalize(expected).ToArray(); | ||
|
||
if (normalizedActual.Length != normalizedExpected.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < normalizedActual.Length; i++) | ||
{ | ||
if (normalizedExpected[i] == "*ignore-line*") | ||
{ | ||
continue; | ||
} | ||
|
||
if (normalizedActual[i] != normalizedExpected[i]) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} | ||
} |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.