Skip to content

Commit d5ec849

Browse files
committed
feat: Adding ability to exclude files
1 parent 022236e commit d5ec849

5 files changed

Lines changed: 271 additions & 56 deletions

File tree

AssetBundleBuilder.Tests/ArgumentParserTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,44 @@ public void Parse_WithRelativePaths_ShouldConvertToAbsolute() {
135135
Assert.Equal(Path.GetFullPath(Path.Combine(currentDir, "Output")), config.OutputDirectory);
136136
}
137137

138+
[Fact]
139+
public void Parse_WithSingleExclude_ShouldAddPattern() {
140+
var assetPath = GetTestPath("Assets");
141+
var outputPath = GetTestPath("Output");
142+
var args = new[] { "2022.3.35f1", assetPath, "testmod", outputPath, "--exclude", "*.tmp" };
143+
var config = ArgumentParser.Parse(args);
144+
145+
Assert.NotNull(config);
146+
Assert.Single(config.ExcludePatterns);
147+
Assert.Contains("*.tmp", config.ExcludePatterns);
148+
}
149+
150+
[Fact]
151+
public void Parse_WithMultipleExcludes_ShouldAddAllPatterns() {
152+
var assetPath = GetTestPath("Assets");
153+
var outputPath = GetTestPath("Output");
154+
var args = new[] { "2022.3.35f1", assetPath, "testmod", outputPath,
155+
"--exclude", "*.tmp", "--exclude", "backup/*", "--exclude", "*.bak" };
156+
var config = ArgumentParser.Parse(args);
157+
158+
Assert.NotNull(config);
159+
Assert.Equal(3, config.ExcludePatterns.Count);
160+
Assert.Contains("*.tmp", config.ExcludePatterns);
161+
Assert.Contains("backup/*", config.ExcludePatterns);
162+
Assert.Contains("*.bak", config.ExcludePatterns);
163+
}
164+
165+
[Fact]
166+
public void Parse_WithoutExclude_ShouldHaveEmptyList() {
167+
var assetPath = GetTestPath("Assets");
168+
var outputPath = GetTestPath("Output");
169+
var args = new[] { "2022.3.35f1", assetPath, "testmod", outputPath };
170+
var config = ArgumentParser.Parse(args);
171+
172+
Assert.NotNull(config);
173+
Assert.Empty(config.ExcludePatterns);
174+
}
175+
138176
private class TempUnityFile : IDisposable {
139177
public TempUnityFile(string baseFileName) {
140178
// Use platform-appropriate Unity executable name
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Reflection;
2+
using System.Text.RegularExpressions;
3+
using Xunit;
4+
5+
namespace CryptikLemur.AssetBundleBuilder.Tests;
6+
7+
public class ExcludePatternTests {
8+
// Use reflection to test private methods
9+
private static bool IsExcluded(string relativePath, List<string> excludePatterns) {
10+
var type = typeof(Program);
11+
var method = type.GetMethod("IsExcluded", BindingFlags.NonPublic | BindingFlags.Static);
12+
return (bool)method!.Invoke(null, new object[] { relativePath, excludePatterns })!;
13+
}
14+
15+
private static string GlobToRegex(string glob) {
16+
var type = typeof(Program);
17+
var method = type.GetMethod("GlobToRegex", BindingFlags.NonPublic | BindingFlags.Static);
18+
return (string)method!.Invoke(null, new object[] { glob })!;
19+
}
20+
21+
[Theory]
22+
[InlineData("*.tmp", "file.tmp", true)]
23+
[InlineData("*.tmp", "file.txt", false)]
24+
[InlineData("*.tmp", "path/to/file.tmp", true)]
25+
[InlineData("backup/*", "backup/file.txt", true)]
26+
[InlineData("backup/*", "other/file.txt", false)]
27+
[InlineData("**/*.log", "deep/nested/path/error.log", true)]
28+
[InlineData("**/*.log", "error.log", true)]
29+
[InlineData("temp*", "temporary.txt", true)]
30+
[InlineData("temp*", "other.txt", false)]
31+
[InlineData("*.bak", "file.bak", true)]
32+
[InlineData("*.bak", "file.backup", false)]
33+
public void IsExcluded_WithVariousPatterns_ShouldMatchCorrectly(string pattern, string path, bool expected) {
34+
var patterns = new List<string> { pattern };
35+
var result = IsExcluded(path, patterns);
36+
Assert.Equal(expected, result);
37+
}
38+
39+
[Fact]
40+
public void IsExcluded_WithMultiplePatterns_ShouldMatchAny() {
41+
var patterns = new List<string> { "*.tmp", "*.bak", "backup/*" };
42+
43+
Assert.True(IsExcluded("file.tmp", patterns));
44+
Assert.True(IsExcluded("file.bak", patterns));
45+
Assert.True(IsExcluded("backup/anything.txt", patterns));
46+
Assert.False(IsExcluded("file.txt", patterns));
47+
}
48+
49+
[Fact]
50+
public void IsExcluded_WithNullPatterns_ShouldReturnFalse() {
51+
Assert.False(IsExcluded("any/path.txt", null!));
52+
}
53+
54+
[Fact]
55+
public void IsExcluded_WithEmptyPatterns_ShouldReturnFalse() {
56+
Assert.False(IsExcluded("any/path.txt", new List<string>()));
57+
}
58+
59+
[Theory]
60+
[InlineData("*.txt", @"^(^|.*/)[^/]*\.txt$")]
61+
[InlineData("backup/*", @"^(^|.*/)backup/[^/]*$")]
62+
[InlineData("**/*.log", @"^(.*/)?[^/]*\.log$")]
63+
[InlineData("/absolute/*.txt", @"^/absolute/[^/]*\.txt$")]
64+
[InlineData("dir/", @"^(^|.*/)dir/.*$")]
65+
public void GlobToRegex_ShouldConvertCorrectly(string glob, string expectedRegex) {
66+
var result = GlobToRegex(glob);
67+
Assert.Equal(expectedRegex, result);
68+
}
69+
70+
[Theory]
71+
[InlineData("file.tmp", "file.tmp")]
72+
[InlineData(@"path\to\file.tmp", "path/to/file.tmp")]
73+
[InlineData(@"C:\Users\test\file.tmp", "C:/Users/test/file.tmp")]
74+
public void IsExcluded_WithWindowsPaths_ShouldNormalize(string windowsPath, string normalizedPath) {
75+
var patterns = new List<string> { "*.tmp" };
76+
77+
// Both should match because paths are normalized
78+
var windowsResult = IsExcluded(windowsPath, patterns);
79+
var normalizedResult = IsExcluded(normalizedPath, patterns);
80+
81+
Assert.True(windowsResult);
82+
Assert.True(normalizedResult);
83+
Assert.Equal(windowsResult, normalizedResult);
84+
}
85+
86+
[Fact]
87+
public void IsExcluded_CaseInsensitive_ShouldMatch() {
88+
var patterns = new List<string> { "*.TMP" };
89+
90+
Assert.True(IsExcluded("file.tmp", patterns));
91+
Assert.True(IsExcluded("FILE.TMP", patterns));
92+
Assert.True(IsExcluded("File.Tmp", patterns));
93+
}
94+
}

AssetBundleBuilder/ArgumentParser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public static class ArgumentParser {
100100
case "--non-interactive":
101101
config.NonInteractive = true;
102102
break;
103+
case "--exclude" when i + 1 < args.Length:
104+
config.ExcludePatterns.Add(args[++i]);
105+
break;
103106
}
104107

105108
if (string.IsNullOrEmpty(config.BundleName)) return null;

0 commit comments

Comments
 (0)