Skip to content

Commit d5a10ae

Browse files
committed
Treat colon as a path separator
1 parent 9f2267c commit d5a10ae

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
- Adjusted globbing so `:` acts like a path separator ([#131](https://github.com/xt0rted/dotnet-run-script/pull/131))
6+
- `foo:*` will match `foo:bar` but not `foo:bar:baz`
7+
- `foo:*:baz` will match `foo:bar:baz`
8+
- `foo:**` will match `foo:bar` and `foo:bar:baz`
9+
510
## [0.5.0](https://github.com/xt0rted/dotnet-run-script/compare/v0.4.0...v0.5.0) - 2022-10-11
611

712
### Added

src/RunScriptCommand.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ internal static List<ScriptResult> FindScripts(
151151

152152
var hadMatch = false;
153153
var matcher = Glob.Parse(
154-
script,
154+
SwapColonAndSlash(script),
155155
new GlobOptions
156156
{
157157
Evaluation =
@@ -162,7 +162,7 @@ internal static List<ScriptResult> FindScripts(
162162

163163
foreach (var projectScript in projectScripts.Keys)
164164
{
165-
if (matcher.IsMatch(projectScript.AsSpan()))
165+
if (matcher.IsMatch(SwapColonAndSlash(projectScript).AsSpan()))
166166
{
167167
hadMatch = true;
168168

@@ -201,4 +201,27 @@ internal static int RunResults(IConsoleWriter writer, List<RunResult> results)
201201

202202
return hadError ? 1 : 0;
203203
}
204+
205+
internal static string SwapColonAndSlash(string scriptName)
206+
{
207+
var result = new char[scriptName.Length];
208+
209+
for (var i = 0; i < scriptName.Length; i++)
210+
{
211+
if (scriptName[i] == ':')
212+
{
213+
result[i] = '/';
214+
}
215+
else if (scriptName[i] == '/')
216+
{
217+
result[i] = ':';
218+
}
219+
else
220+
{
221+
result[i] = scriptName[i];
222+
}
223+
}
224+
225+
return new string(result);
226+
}
204227
}

test/RunScriptCommandTests.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ public FindScripts()
2323
{ "prepack", "echo pack" },
2424
{ "pack", "echo pack" },
2525
{ "postpack", "echo pack" },
26+
{ "foo", "foo" },
27+
{ "foo:foo", "foo:foo" },
28+
{ "foo:bar", "foo:bar" },
29+
{ "foo:baz", "foo:baz" },
30+
{ "foo:foo:foo", "foo:foo:foo" },
31+
{ "foo:foo:bar", "foo:foo:bar" },
32+
{ "foo:foo:baz", "foo:foo:baz" },
33+
{ "foo:bar:foo", "foo:bar:foo" },
34+
{ "foo:bar:bar", "foo:bar:bar" },
35+
{ "foo:bar:baz", "foo:bar:baz" },
36+
{ "foo:baz:foo", "foo:baz:foo" },
37+
{ "foo:baz:bar", "foo:baz:bar" },
38+
{ "foo:baz:baz", "foo:baz:baz" },
2639
};
2740
}
2841

@@ -58,6 +71,7 @@ public void Should_match_wildcard_script_names()
5871
// Given
5972
var scripts = new[]
6073
{
74+
"foo*",
6175
"pre*",
6276
"magic*",
6377
};
@@ -71,11 +85,93 @@ public void Should_match_wildcard_script_names()
7185
result.ShouldBe(
7286
new List<ScriptResult>
7387
{
88+
new("foo", true),
7489
new("prebuild", true),
7590
new("prepack", true),
7691
new("magic*", false),
7792
});
7893
}
94+
95+
[Fact]
96+
public void Should_match_only_1_segment()
97+
{
98+
// Given
99+
var scripts = new[]
100+
{
101+
"foo:*"
102+
};
103+
104+
// When
105+
var result = RunScriptCommand.FindScripts(
106+
_projectScripts,
107+
scripts);
108+
109+
// Then
110+
result.ShouldBe(
111+
new List<ScriptResult>
112+
{
113+
new("foo:foo", true),
114+
new("foo:bar", true),
115+
new("foo:baz", true),
116+
});
117+
}
118+
119+
[Fact]
120+
public void Should_match_only_1_trailing_segment()
121+
{
122+
// Given
123+
var scripts = new[]
124+
{
125+
"foo:bar:*"
126+
};
127+
128+
// When
129+
var result = RunScriptCommand.FindScripts(
130+
_projectScripts,
131+
scripts);
132+
133+
// Then
134+
result.ShouldBe(
135+
new List<ScriptResult>
136+
{
137+
new("foo:bar:foo", true),
138+
new("foo:bar:bar", true),
139+
new("foo:bar:baz", true),
140+
});
141+
}
142+
143+
[Fact]
144+
public void Should_match_multiple_segments()
145+
{
146+
// Given
147+
var scripts = new[]
148+
{
149+
"foo:**"
150+
};
151+
152+
// When
153+
var result = RunScriptCommand.FindScripts(
154+
_projectScripts,
155+
scripts);
156+
157+
// Then
158+
result.ShouldBe(
159+
new List<ScriptResult>
160+
{
161+
new("foo:foo", true),
162+
new("foo:bar", true),
163+
new("foo:baz", true),
164+
new("foo:foo:foo", true),
165+
new("foo:foo:bar", true),
166+
new("foo:foo:baz", true),
167+
new("foo:bar:foo", true),
168+
new("foo:bar:bar", true),
169+
new("foo:bar:baz", true),
170+
new("foo:baz:foo", true),
171+
new("foo:baz:bar", true),
172+
new("foo:baz:baz", true),
173+
});
174+
}
79175
}
80176

81177
[Trait("category", "unit")]

0 commit comments

Comments
 (0)