Skip to content

Commit 4ae30f3

Browse files
moljacdevlead
andauthored
File type check on macosx (if file is "dylib") (#4206)
* added file type check on macosx (if file is "dylib" AKA has "dylib" extension) * Added unit test and fixes for empty path * refactored to test Path and suffix ("extension") GetExtension throws * inclusively covering all PE extensions (not excluding dylib only) * fixing tests accoring new logic (reverting to old) * style fixes * Update src/Cake.Core/IO/FileExtensions.cs Co-authored-by: Mattias Karlsson <mattias@devlead.se> * Add `.` to extensions Co-authored-by: Mattias Karlsson <mattias@devlead.se> --------- Co-authored-by: Mattias Karlsson <mattias@devlead.se>
1 parent 2ab3fb4 commit 4ae30f3

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/Cake.Core.Tests/Unit/IO/FileExtensionsTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,21 @@ public void Should_Return_False_When_File_Is_Not_A_Valid_PE()
297297
// Then
298298
Assert.False(FileExtensions.IsClrAssembly(file));
299299
}
300+
301+
[Fact]
302+
public void Should_Return_False_When_File_Is_MacOS_MachO_dylib()
303+
{
304+
// Given
305+
var file = Substitute.For<IFile>();
306+
307+
// When
308+
file.Exists.Returns(true);
309+
file.Path.Returns(new string("fullname.dylib"));
310+
file.Length.Returns(_invalidPeBytes.Length);
311+
312+
// Then
313+
Assert.False(FileExtensions.IsClrAssembly(file));
314+
}
300315
}
301316
}
302317
}

src/Cake.Core/IO/FileExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ public static bool IsClrAssembly(this IFile file)
116116
return false;
117117
}
118118

119+
// Is known extension?
120+
switch (file.Path?.GetExtension().ToLowerInvariant())
121+
{
122+
case ".dll":
123+
case ".exe":
124+
case ".sys":
125+
case ".tsp":
126+
case ".acm":
127+
case ".ax":
128+
case ".cpl":
129+
case ".drv":
130+
case ".efi":
131+
case ".mui":
132+
case ".ocx":
133+
case ".scr":
134+
case null:
135+
break;
136+
137+
default:
138+
return false;
139+
}
140+
119141
using (var fs = file.OpenRead())
120142
{
121143
using (var reader = new System.IO.BinaryReader(fs))

0 commit comments

Comments
 (0)