Skip to content

Commit cf913cf

Browse files
committed
Merge branch 'Kemyke-fix_load_absolute_path_legacy_format' into develop
* Kemyke-fix_load_absolute_path_legacy_format: Change the legacy pattern validation. Do not rely on Uri.TryCreate, use a regex instead. This will fix the absolute path handling issues. Add tests for testing legacy and new patterns with relative and absolute pathes on all platform.
2 parents ddb5835 + 1d94dda commit cf913cf

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

src/Cake.Core.Tests/Unit/Scripting/Processors/LoadDirectiveProcessorTests.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Cake.Core.Scripting.Processors.Loading;
66
using Cake.Core.Tests.Fixtures;
7+
using Cake.Testing.Xunit;
78
using Xunit;
89

910
namespace Cake.Core.Tests.Unit.Scripting.Processors
@@ -137,5 +138,81 @@ public void Should_Insert_Line_Directives_When_Processing_Load_Directives()
137138
Assert.Equal(result.Lines[11], "// #l b.cake");
138139
Assert.Equal(result.Lines[12], "int y=2;");
139140
}
141+
142+
[Theory]
143+
[InlineData("#load \"/utils.cake\"")]
144+
[InlineData("#load \"local:?path=/utils.cake\"")]
145+
public void Should_Process_AbsolutePath_Script_Reference_Found_In_Source(string source)
146+
{
147+
// Given
148+
var fixture = new ScriptAnalyzerFixture();
149+
fixture.Providers.Add(new FileLoadDirectiveProvider());
150+
fixture.GivenScriptExist("/Working/script.cake", source);
151+
fixture.GivenScriptExist("/utils.cake", "Console.WriteLine();");
152+
153+
// When
154+
var result = fixture.Analyze("/Working/script.cake");
155+
156+
// Then
157+
Assert.Equal(1, result.Script.Includes.Count);
158+
Assert.Equal("/utils.cake", result.Script.Includes[0].Path.FullPath);
159+
}
160+
161+
[Theory]
162+
[InlineData("#load \"test/utils.cake\"")]
163+
[InlineData("#load \"local:?path=test/utils.cake\"")]
164+
public void Should_Process_RelativePath_Script_Reference_Found_In_Source(string source)
165+
{
166+
// Given
167+
var fixture = new ScriptAnalyzerFixture();
168+
fixture.Providers.Add(new FileLoadDirectiveProvider());
169+
fixture.GivenScriptExist("/Working/script.cake", source);
170+
fixture.GivenScriptExist("/Working/test/utils.cake", "Console.WriteLine();");
171+
172+
// When
173+
var result = fixture.Analyze("/Working/script.cake");
174+
175+
// Then
176+
Assert.Equal(1, result.Script.Includes.Count);
177+
Assert.Equal("/Working/test/utils.cake", result.Script.Includes[0].Path.FullPath);
178+
}
179+
180+
[WindowsTheory]
181+
[InlineData("#load \"c:/utils.cake\"")]
182+
[InlineData("#load \"local:?path=c:/utils.cake\"")]
183+
public void Should_Process_WindowsAbsolutePath_Script_Reference_Found_In_Source(string source)
184+
{
185+
// Given
186+
var fixture = new ScriptAnalyzerFixture();
187+
fixture.Providers.Add(new FileLoadDirectiveProvider());
188+
fixture.GivenScriptExist("/Working/script.cake", source);
189+
fixture.GivenScriptExist("c:/utils.cake", "Console.WriteLine();");
190+
191+
// When
192+
var result = fixture.Analyze("/Working/script.cake");
193+
194+
// Then
195+
Assert.Equal(1, result.Script.Includes.Count);
196+
Assert.Equal("c:/utils.cake", result.Script.Includes[0].Path.FullPath);
197+
}
198+
199+
[WindowsTheory]
200+
[InlineData("#load \"test/utils.cake\"")]
201+
[InlineData("#load \"local:?path=test/utils.cake\"")]
202+
public void Should_Process_WindowsRelativePath_Script_Reference_Found_In_Source(string source)
203+
{
204+
// Given
205+
var fixture = new ScriptAnalyzerFixture();
206+
fixture.Providers.Add(new FileLoadDirectiveProvider());
207+
fixture.GivenScriptExist("/Working/script.cake", source);
208+
fixture.GivenScriptExist("/Working/test/utils.cake", "Console.WriteLine();");
209+
210+
// When
211+
var result = fixture.Analyze("/Working/script.cake");
212+
213+
// Then
214+
Assert.Equal(1, result.Script.Includes.Count);
215+
Assert.Equal("/Working/test/utils.cake", result.Script.Includes[0].Path.FullPath);
216+
}
140217
}
141218
}

src/Cake.Core/Scripting/Processors/UriDirectiveProcessor.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Text;
9+
using System.Text.RegularExpressions;
910
using Cake.Core.Scripting.Analysis;
1011

1112
namespace Cake.Core.Scripting.Processors
1213
{
1314
internal abstract class UriDirectiveProcessor : LineProcessor
1415
{
16+
private readonly Regex _uriPrefixPattern;
17+
1518
protected abstract IEnumerable<string> GetDirectiveNames();
1619

1720
protected abstract void AddToContext(IScriptAnalyzerContext context, Uri uri);
1821

22+
protected UriDirectiveProcessor()
23+
{
24+
_uriPrefixPattern = new Regex("^([a-zA-Z]{2,}:)");
25+
}
26+
1927
public sealed override bool Process(IScriptAnalyzerContext context, string line, out string replacement)
2028
{
2129
if (context == null)
@@ -60,13 +68,22 @@ public sealed override bool Process(IScriptAnalyzerContext context, string line,
6068
private Uri ParseUriFromTokens(string[] tokens)
6169
{
6270
Uri uri;
63-
if (!Uri.TryCreate(tokens[1].UnQuote(), UriKind.Absolute, out uri))
71+
if (IsUriFromLegacyPattern(tokens))
6472
{
6573
uri = CreateUriFromLegacyFormat(tokens);
6674
}
75+
else
76+
{
77+
uri = new Uri(tokens[1].UnQuote(), UriKind.Absolute);
78+
}
6779
return uri;
6880
}
6981

82+
private bool IsUriFromLegacyPattern(string[] tokens)
83+
{
84+
return !_uriPrefixPattern.IsMatch(tokens[1].UnQuote());
85+
}
86+
7087
protected virtual Uri CreateUriFromLegacyFormat(string[] tokens)
7188
{
7289
var builder = new StringBuilder();

0 commit comments

Comments
 (0)