Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions FileInspectorX.Tests/SettingsBehaviorTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using FileInspectorX;
using System.Collections.Generic;
using System.Diagnostics;
#if NET8_0_OR_GREATER
using System.Reflection;
using System.Runtime.Loader;
#endif
using System.Threading;
using FileInspectorX;

namespace FileInspectorX.Tests;

[Xunit.Collection(nameof(DetectionSettingsCollection))]
public class SettingsBehaviorTests
{
{
#if NET8_0_OR_GREATER
[Xunit.Fact]
public void Breadcrumbs_DoNotRequireProcessAssembly()
{
var logPath = Path.Combine(Path.GetTempPath(), $"FileInspectorX.Breadcrumbs.{Guid.NewGuid():N}.log");
var loadContext = new ProcessRejectingLoadContext();
try
{
var assembly = loadContext.LoadFromAssemblyPath(typeof(FileInspector).Assembly.Location);
var settings = assembly.GetType("FileInspectorX.Settings", throwOnError: true)!;
settings.GetProperty("BreadcrumbsEnabled")!.SetValue(null, true);
settings.GetProperty("BreadcrumbsPath")!.SetValue(null, logPath);
var breadcrumbs = assembly.GetType("FileInspectorX.Breadcrumbs", throwOnError: true)!;
var write = breadcrumbs.GetMethod("Write", BindingFlags.Static | BindingFlags.NonPublic)!;

write.Invoke(null, new object?[] { "ISOLATED_TEST", null, null });

Xunit.Assert.Contains($"PID={Environment.ProcessId}", File.ReadAllText(logPath), StringComparison.Ordinal);
}
finally
{
loadContext.Unload();
File.Delete(logPath);
}
}
#endif

[Xunit.Fact]
public void JsonValidationCore_TimesOut_When_StopwatchExpired()
{
Expand Down Expand Up @@ -93,5 +124,18 @@ public void DangerousExtensionsOverrideMode_Merge_Keeps_Defaults()
Settings.DangerousExtensionsOverride = prevOverride;
Settings.DangerousExtensionsOverrideMode = prevMode;
}
}
}
}

#if NET8_0_OR_GREATER
private sealed class ProcessRejectingLoadContext() : AssemblyLoadContext(isCollectible: true)
{
protected override Assembly? Load(AssemblyName assemblyName)
{
if (string.Equals(assemblyName.Name, "System.Diagnostics.Process", StringComparison.Ordinal))
throw new FileNotFoundException("Process assembly is unavailable in the isolated test context.", assemblyName.Name);

return null;
}
}
#endif
}
2 changes: 1 addition & 1 deletion FileInspectorX/FileInspectorX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Description>FileInspectorX: dependency-free content type detection (magic bytes + heuristics)</Description>
<AssemblyName>FileInspectorX</AssemblyName>
<VersionPrefix>1.1.1</VersionPrefix>
<VersionPrefix>1.1.2</VersionPrefix>
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
<PackageReadmeFile>README.MD</PackageReadmeFile>
<PackageProjectUrl>https://github.com/EvotecIT/FileInspectorX</PackageProjectUrl>
Expand Down
5 changes: 4 additions & 1 deletion FileInspectorX/Logging/Breadcrumbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ internal static void Write(string tag, string? message = null, string? path = nu
}
} catch { }
var now = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fffZ");
#if NET8_0_OR_GREATER
var pid = Environment.ProcessId;
#else
var pid = Process.GetCurrentProcess().Id;
#endif
var tid = Environment.CurrentManagedThreadId;
var line = $"{now} PID={pid} TID={tid} TAG={tag}{(path!=null?" PATH="+path:"")}{(message!=null?" MSG="+message:"")}";
File.AppendAllText(file, line + Environment.NewLine);
}
catch { /* never throw from breadcrumbs */ }
}
}