diff --git a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj
index cfd14ed33..240ebbf0b 100644
--- a/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj
+++ b/src/KubernetesClient.Classic/KubernetesClient.Classic.csproj
@@ -7,7 +7,6 @@
-
@@ -19,7 +18,7 @@
-
+
diff --git a/src/KubernetesClient/CertUtils.cs b/src/KubernetesClient/CertUtils.cs
index ad4fad90a..21f369927 100644
--- a/src/KubernetesClient/CertUtils.cs
+++ b/src/KubernetesClient/CertUtils.cs
@@ -24,7 +24,7 @@ internal static class CertUtils
public static X509Certificate2Collection LoadPemFileCert(string file)
{
var certCollection = new X509Certificate2Collection();
- using (var stream = FileUtils.FileSystem().File.OpenRead(file))
+ using (var stream = FileSystem.Current.OpenRead(file))
{
#if NET5_0_OR_GREATER
certCollection.ImportFromPem(new StreamReader(stream).ReadToEnd());
diff --git a/src/KubernetesClient/FileSystem.cs b/src/KubernetesClient/FileSystem.cs
new file mode 100644
index 000000000..22736ef98
--- /dev/null
+++ b/src/KubernetesClient/FileSystem.cs
@@ -0,0 +1,57 @@
+using System.IO;
+
+namespace k8s
+{
+ internal static class FileSystem
+ {
+ public interface IFileSystem
+ {
+ Stream OpenRead(string path);
+
+ bool Exists(string path);
+
+ string ReadAllText(string path);
+ }
+
+ public static IFileSystem Current { get; private set; } = new RealFileSystem();
+
+ public static IDisposable With(IFileSystem fileSystem)
+ {
+ return new InjectedFileSystem(fileSystem);
+ }
+
+ private class InjectedFileSystem : IDisposable
+ {
+ private readonly IFileSystem _original;
+
+ public InjectedFileSystem(IFileSystem fileSystem)
+ {
+ _original = Current;
+ Current = fileSystem;
+ }
+
+ public void Dispose()
+ {
+ Current = _original;
+ }
+ }
+
+ private class RealFileSystem : IFileSystem
+ {
+ public bool Exists(string path)
+ {
+ return File.Exists(path);
+ }
+
+ public Stream OpenRead(string path)
+ {
+ return File.OpenRead(path);
+ }
+
+ public string ReadAllText(string path)
+ {
+ return File.ReadAllText(path);
+ }
+ }
+ }
+}
diff --git a/src/KubernetesClient/FileUtils.cs b/src/KubernetesClient/FileUtils.cs
deleted file mode 100644
index 61fca2f1f..000000000
--- a/src/KubernetesClient/FileUtils.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-using System.IO.Abstractions;
-
-namespace k8s
-{
- internal static class FileUtils
- {
- private static readonly IFileSystem RealFileSystem = new FileSystem();
- private static IFileSystem currentFileSystem = null;
-
- public static void InjectFilesystem(IFileSystem fs)
- {
- currentFileSystem = fs;
- }
-
- public static IFileSystem FileSystem()
- {
- return currentFileSystem != null ? currentFileSystem : RealFileSystem;
- }
-
- public sealed class InjectedFileSystem : IDisposable
- {
- public InjectedFileSystem(IFileSystem fs)
- {
- InjectFilesystem(fs);
- }
-
- public void Dispose()
- {
- InjectFilesystem(null);
- }
- }
- }
-}
diff --git a/src/KubernetesClient/KubernetesClient.csproj b/src/KubernetesClient/KubernetesClient.csproj
index 60f79d565..8af824522 100644
--- a/src/KubernetesClient/KubernetesClient.csproj
+++ b/src/KubernetesClient/KubernetesClient.csproj
@@ -8,7 +8,6 @@
-
diff --git a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs
index 510744241..41df24731 100644
--- a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs
+++ b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs
@@ -30,13 +30,13 @@ public static bool IsInCluster()
}
var tokenPath = Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName);
- if (!FileUtils.FileSystem().File.Exists(tokenPath))
+ if (!FileSystem.Current.Exists(tokenPath))
{
return false;
}
var certPath = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName);
- return FileUtils.FileSystem().File.Exists(certPath);
+ return FileSystem.Current.Exists(certPath);
}
public static KubernetesClientConfiguration InClusterConfig()
@@ -68,9 +68,9 @@ public static KubernetesClientConfiguration InClusterConfig()
};
var namespaceFile = Path.Combine(ServiceAccountPath, ServiceAccountNamespaceFileName);
- if (FileUtils.FileSystem().File.Exists(namespaceFile))
+ if (FileSystem.Current.Exists(namespaceFile))
{
- result.Namespace = FileUtils.FileSystem().File.ReadAllText(namespaceFile);
+ result.Namespace = FileSystem.Current.ReadAllText(namespaceFile);
}
return result;
diff --git a/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs b/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
index 7d23404e8..ed664fb3c 100644
--- a/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
+++ b/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Runtime.InteropServices;
@@ -694,6 +695,31 @@ private static void AssertUserEqual(User expected, User actual)
}
}
+ private class FileSystemAdapter : FileSystem.IFileSystem
+ {
+ private readonly IFileSystem io;
+
+ public FileSystemAdapter(System.IO.Abstractions.IFileSystem io)
+ {
+ this.io = io;
+ }
+
+ public bool Exists(string path)
+ {
+ return io.File.Exists(path);
+ }
+
+ public Stream OpenRead(string path)
+ {
+ return io.File.OpenRead(path);
+ }
+
+ public string ReadAllText(string path)
+ {
+ return io.File.ReadAllText(path);
+ }
+ }
+
///
/// Test in cluster configuration.
///
@@ -713,7 +739,7 @@ public void IsInCluster()
{ tokenPath, new MockFileData("foo") },
{ certPath, new MockFileData("bar") },
});
- using (new FileUtils.InjectedFileSystem(fileSystem))
+ using (FileSystem.With(new FileSystemAdapter(fileSystem)))
{
Assert.True(KubernetesClientConfiguration.IsInCluster());
}
@@ -737,7 +763,7 @@ public void LoadInCluster()
{ certPath, new MockFileData("bar") },
});
- using (new FileUtils.InjectedFileSystem(fileSystem))
+ using (FileSystem.With(new FileSystemAdapter(fileSystem)))
{
var config = KubernetesClientConfiguration.InClusterConfig();
Assert.Equal("https://other.default.svc:443/", config.Host);
@@ -764,7 +790,7 @@ public void LoadInClusterNamespace()
{ namespacePath, new MockFileData("some namespace") },
});
- using (new FileUtils.InjectedFileSystem(fileSystem))
+ using (FileSystem.With(new FileSystemAdapter(fileSystem)))
{
var config = KubernetesClientConfiguration.InClusterConfig();
Assert.Equal("https://kubernetes.default.svc:443/", config.Host);