From 5cf18fb1126426bada22db448659bfdb8e1b5306 Mon Sep 17 00:00:00 2001 From: Fyodor Sobolev <117388856+fsobolev@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:35:42 +0300 Subject: [PATCH 1/4] Add SystemDirectories --- .../SystemDirectoriesTest.cs | 37 ++++++++++++ Nickvision.Aura/SystemDirectories.cs | 60 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 Nickvision.Aura.Tests/SystemDirectoriesTest.cs create mode 100644 Nickvision.Aura/SystemDirectories.cs diff --git a/Nickvision.Aura.Tests/SystemDirectoriesTest.cs b/Nickvision.Aura.Tests/SystemDirectoriesTest.cs new file mode 100644 index 0000000..eb2e1d8 --- /dev/null +++ b/Nickvision.Aura.Tests/SystemDirectoriesTest.cs @@ -0,0 +1,37 @@ +using System.Runtime.InteropServices; +using Xunit.Abstractions; + +namespace Nickvision.Aura.Tests; + +public class SystemDirectoriesTest +{ + private readonly ITestOutputHelper _output; + + public SystemDirectoriesTest(ITestOutputHelper output) + { + _output = output; + } + + [Fact] + public void Path() + { + _output.WriteLine($"Path: {string.Join(", ", SystemDirectories.Path)}"); + Assert.True(SystemDirectories.Path.Length > 0); + } + + [SkippableFact] + public void Config() + { + Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); + _output.WriteLine($"Config: {string.Join(", ", SystemDirectories.Config)}"); + Assert.True(SystemDirectories.Config.Length > 0); + } + + [SkippableFact] + public void Data() + { + Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); + _output.WriteLine($"Data: {string.Join(", ", SystemDirectories.Data)}"); + Assert.True(SystemDirectories.Data.Length > 0); + } +} \ No newline at end of file diff --git a/Nickvision.Aura/SystemDirectories.cs b/Nickvision.Aura/SystemDirectories.cs new file mode 100644 index 0000000..dc6526a --- /dev/null +++ b/Nickvision.Aura/SystemDirectories.cs @@ -0,0 +1,60 @@ +using System; +using System.Linq; +using System.Runtime.InteropServices; + +namespace Nickvision.Aura; + +/// +/// System directories paths +/// +public class SystemDirectories { + private static string[]? _path; + private static string[]? _config; + private static string[]? _data; + + /// + /// Array of paths from PATH variable + /// + public static string[] Path + { + get + { + _path ??= Environment.GetEnvironmentVariable("PATH")?.Split(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ';' : ':').ToArray() ?? Array.Empty(); + return _path; + } + } + + /// + /// Array of paths from XDG_CONFIG_DIRS + /// + /// Thrown if used not on Linux + public static string[] Config + { + get + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + throw new PlatformNotSupportedException(); + } + _config ??= Environment.GetEnvironmentVariable("XDG_CONFIG_DIRS")?.Split(':').ToArray() ?? Array.Empty(); + return _config; + } + } + + /// + /// Array of paths from XDG_DATA_DIRS + /// + /// Thrown if used not on Linux + public static string[] Data + { + get + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + throw new PlatformNotSupportedException(); + } + _data ??= Environment.GetEnvironmentVariable("XDG_DATA_DIRS")?.Split(':').ToArray() ?? Array.Empty(); + return _data; + } + } +} \ No newline at end of file From f36b695ad75d75d966c645d4fda9a663242d0dcc Mon Sep 17 00:00:00 2001 From: Fyodor Sobolev <117388856+fsobolev@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:43:28 +0300 Subject: [PATCH 2/4] Skip tests in github actions --- Nickvision.Aura.Tests/SystemDirectoriesTest.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Nickvision.Aura.Tests/SystemDirectoriesTest.cs b/Nickvision.Aura.Tests/SystemDirectoriesTest.cs index eb2e1d8..8969ff2 100644 --- a/Nickvision.Aura.Tests/SystemDirectoriesTest.cs +++ b/Nickvision.Aura.Tests/SystemDirectoriesTest.cs @@ -1,3 +1,4 @@ +using System; using System.Runtime.InteropServices; using Xunit.Abstractions; @@ -23,6 +24,7 @@ public void Path() public void Config() { Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); + Skip.IfNot(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"))); _output.WriteLine($"Config: {string.Join(", ", SystemDirectories.Config)}"); Assert.True(SystemDirectories.Config.Length > 0); } @@ -31,6 +33,7 @@ public void Config() public void Data() { Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); + Skip.IfNot(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"))); _output.WriteLine($"Data: {string.Join(", ", SystemDirectories.Data)}"); Assert.True(SystemDirectories.Data.Length > 0); } From 54a3a2f0c2e765080bc354eb63adfd8ff8807237 Mon Sep 17 00:00:00 2001 From: Fyodor Sobolev <117388856+fsobolev@users.noreply.github.com> Date: Fri, 22 Sep 2023 18:26:21 +0300 Subject: [PATCH 3/4] System Dirs - Don't throw exception on unsupported platform --- Nickvision.Aura/SystemDirectories.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Nickvision.Aura/SystemDirectories.cs b/Nickvision.Aura/SystemDirectories.cs index dc6526a..9e36dd9 100644 --- a/Nickvision.Aura/SystemDirectories.cs +++ b/Nickvision.Aura/SystemDirectories.cs @@ -27,14 +27,14 @@ public static string[] Path /// /// Array of paths from XDG_CONFIG_DIRS /// - /// Thrown if used not on Linux + /// Returns empty array when accessed not on Linux public static string[] Config { get { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - throw new PlatformNotSupportedException(); + return Array.Empty(); } _config ??= Environment.GetEnvironmentVariable("XDG_CONFIG_DIRS")?.Split(':').ToArray() ?? Array.Empty(); return _config; @@ -44,14 +44,14 @@ public static string[] Config /// /// Array of paths from XDG_DATA_DIRS /// - /// Thrown if used not on Linux + /// Returns empty array when accessed not on Linux public static string[] Data { get { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - throw new PlatformNotSupportedException(); + return Array.Empty(); } _data ??= Environment.GetEnvironmentVariable("XDG_DATA_DIRS")?.Split(':').ToArray() ?? Array.Empty(); return _data; From e405177368feabdcd0f7b36d956e064f5dc394f1 Mon Sep 17 00:00:00 2001 From: Fyodor Sobolev <117388856+fsobolev@users.noreply.github.com> Date: Fri, 22 Sep 2023 18:26:40 +0300 Subject: [PATCH 4/4] System Dirs - Better tests --- .../SystemDirectoriesTest.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Nickvision.Aura.Tests/SystemDirectoriesTest.cs b/Nickvision.Aura.Tests/SystemDirectoriesTest.cs index 8969ff2..f0fea1f 100644 --- a/Nickvision.Aura.Tests/SystemDirectoriesTest.cs +++ b/Nickvision.Aura.Tests/SystemDirectoriesTest.cs @@ -1,40 +1,40 @@ using System; +using System.Linq; using System.Runtime.InteropServices; -using Xunit.Abstractions; namespace Nickvision.Aura.Tests; public class SystemDirectoriesTest { - private readonly ITestOutputHelper _output; - - public SystemDirectoriesTest(ITestOutputHelper output) - { - _output = output; - } - [Fact] public void Path() { - _output.WriteLine($"Path: {string.Join(", ", SystemDirectories.Path)}"); - Assert.True(SystemDirectories.Path.Length > 0); + var path = new []{ "test0", "test1" }; + Environment.SetEnvironmentVariable("PATH", string.Join(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ";" : ":", path)); + Assert.True(SystemDirectories.Path.SequenceEqual(path)); } - [SkippableFact] + [Fact] public void Config() { - Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); - Skip.IfNot(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"))); - _output.WriteLine($"Config: {string.Join(", ", SystemDirectories.Config)}"); - Assert.True(SystemDirectories.Config.Length > 0); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Assert.True(SystemDirectories.Config.Length == 0); + } + var config = new []{ "test0", "test1" }; + Environment.SetEnvironmentVariable("XDG_CONFIG_DIRS", string.Join(":", config)); + Assert.True(SystemDirectories.Config.SequenceEqual(config)); } - [SkippableFact] + [Fact] public void Data() { - Skip.IfNot(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); - Skip.IfNot(string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"))); - _output.WriteLine($"Data: {string.Join(", ", SystemDirectories.Data)}"); - Assert.True(SystemDirectories.Data.Length > 0); + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Assert.True(SystemDirectories.Data.Length == 0); + } + var data = new []{ "test0", "test1" }; + Environment.SetEnvironmentVariable("XDG_DATA_DIRS", string.Join(":", data)); + Assert.True(SystemDirectories.Data.SequenceEqual(data)); } } \ No newline at end of file