-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit NuGet description length (#80)
- Loading branch information
Showing
9 changed files
with
260 additions
and
7 deletions.
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
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
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
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
117 changes: 117 additions & 0 deletions
117
Source/Bake.Tests/UnitTests/Services/DescriptionLimiterTests.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,117 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2021-2022 Rasmus Mikkelsen | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
using System; | ||
using Bake.Services; | ||
using Bake.Tests.Helpers; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
// ReSharper disable StringLiteralTypo | ||
|
||
namespace Bake.Tests.UnitTests.Services | ||
{ | ||
public class DescriptionLimiterTests : TestFor<DescriptionLimiter> | ||
{ | ||
[Test] | ||
public void LessThanMaxIsAccepted() | ||
{ | ||
// Arrange | ||
var text = A<string>(); | ||
|
||
// Act | ||
var output = Sut.Limit(text, int.MaxValue); | ||
|
||
// Assert | ||
output.Should().Be(text); | ||
} | ||
|
||
[Test] | ||
public void CutsAtHeadlineSimple() | ||
{ | ||
// Arrange | ||
var text = Concat( | ||
"#", | ||
new string('x', 5), | ||
"#", | ||
new string('y', 10)); | ||
|
||
// Act | ||
var output = Sut.Limit(text, 15); | ||
|
||
// Assert | ||
AssertEquals( | ||
output, | ||
"#", | ||
new string('x', 5)); | ||
} | ||
|
||
[Test] | ||
public void CutsAtHeadline() | ||
{ | ||
// Arrange | ||
var text = Concat( | ||
"#", | ||
new string('x', 10), | ||
"#", | ||
new string('y', 10), | ||
"#", | ||
new string('z', 10), | ||
"#", | ||
new string('i', 10)); | ||
|
||
// Act | ||
var output = Sut.Limit(text, 36); | ||
|
||
// Assert | ||
AssertEquals( | ||
output, | ||
"#", | ||
new string('x', 10), | ||
"#", | ||
new string('y', 10)); | ||
} | ||
|
||
[Test] | ||
public void Fallback() | ||
{ | ||
// Arrange | ||
var text = new string('x', 42); | ||
|
||
// Act | ||
var output = Sut.Limit(text, 40); | ||
|
||
// Assert | ||
output.Should().Be(new string('x', 40)); | ||
} | ||
|
||
private static string Concat(params string[] texts) => string.Join(Environment.NewLine, texts); | ||
|
||
private static void AssertEquals( | ||
string input, | ||
params string[] expected) | ||
{ | ||
var inputLines = input.Split(new[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); | ||
inputLines.Should().BeEquivalentTo(expected, o => o.WithStrictOrdering()); | ||
} | ||
} | ||
} |
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
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
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,97 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2021-2022 Rasmus Mikkelsen | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Bake.Services | ||
{ | ||
public class DescriptionLimiter : IDescriptionLimiter | ||
{ | ||
private readonly ILogger<DescriptionLimiter> _logger; | ||
|
||
public DescriptionLimiter( | ||
ILogger<DescriptionLimiter> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public string Limit(string markdown, int maxLength) | ||
{ | ||
if (string.IsNullOrEmpty(markdown) || | ||
markdown.Length <= maxLength) | ||
{ | ||
return markdown ?? string.Empty; | ||
} | ||
|
||
var lines = markdown | ||
.Split('\n', '\r'); | ||
var totalHeadlines = lines.Count(l => l.StartsWith("#")); | ||
|
||
for (var i = totalHeadlines; 0 <= i; i--) | ||
{ | ||
var text = string.Join(Environment.NewLine, TakeHeadlines(lines, i)); | ||
if (string.IsNullOrWhiteSpace(text)) | ||
{ | ||
break; | ||
} | ||
if (text.Length <= maxLength) | ||
{ | ||
_logger.LogWarning( | ||
"Description text with its length of {CurrentLength} is too long compared to the maximum {MaximumLength}, cutting it at closest header to {NewLength}", | ||
markdown.Length, | ||
maxLength, | ||
text.Length); | ||
return text; | ||
} | ||
} | ||
|
||
_logger.LogWarning( | ||
"Description text with its length of {CurrentLength} is too long compared to the maximum {MaximumLength}, but cannot find a good markdown header to cut at so cutting it in the middle of text at {NewLength}", | ||
markdown.Length, | ||
maxLength, | ||
maxLength); | ||
|
||
return markdown[..maxLength]; | ||
} | ||
|
||
private static IEnumerable<string> TakeHeadlines(IEnumerable<string> lines, int headlines) | ||
{ | ||
var currentHeadline = 0; | ||
foreach (var line in lines) | ||
{ | ||
if (line.StartsWith("#")) | ||
{ | ||
currentHeadline++; | ||
} | ||
if (headlines < currentHeadline) | ||
{ | ||
yield break; | ||
} | ||
|
||
yield return line; | ||
} | ||
} | ||
} | ||
} |
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,29 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2021-2022 Rasmus Mikkelsen | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
namespace Bake.Services | ||
{ | ||
public interface IDescriptionLimiter | ||
{ | ||
string Limit(string markdown, int maxLength); | ||
} | ||
} |