diff --git a/src/Pacserver.Tests/TransferFilesTest.cs b/src/Pacserver.Tests/TransferFilesTest.cs index 52905f8..4de7999 100644 --- a/src/Pacserver.Tests/TransferFilesTest.cs +++ b/src/Pacserver.Tests/TransferFilesTest.cs @@ -4,10 +4,15 @@ namespace Pacserver.Tests; public class TranserFilesTest { [Fact] - public void TransferPacmanCacheTest() { - string result = PacserverUtils.determinePacmanCacheDirectory(); - PacserverUtils.TransferPacmanCache(); + public void transferPacmanCache_doesNotFail() { + // Arrange + PacserverUtils utils = new PacserverUtils(); + // Act + utils.readPacmanConfig(); + utils.transferPacmanCache(); + + // Assert //Assert.NotEmpty(Directory.GetFiles("/home/rene/test/")); } } diff --git a/src/Pacserver.Tests/pacmanCache_Test.cs b/src/Pacserver.Tests/pacmanCache_Test.cs deleted file mode 100644 index 9305fd5..0000000 --- a/src/Pacserver.Tests/pacmanCache_Test.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Pacserver.Utils; - -namespace Pacserver.Tests; - -public class pacmanCache_Test { - [Fact] - public void doesPacmanCacheExist() { - string result = PacserverUtils.determinePacmanCacheDirectory(); - - Assert.Equivalent(result, "/var/cache/pacman/pkg/"); - } -} - diff --git a/src/Pacserver.Tests/pacmanDatabase_Test.cs b/src/Pacserver.Tests/pacmanDatabase_Test.cs deleted file mode 100644 index 8b08cd4..0000000 --- a/src/Pacserver.Tests/pacmanDatabase_Test.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Pacserver.Utils; - -namespace Pacserver.Tests; - -public class pacmanDatabase_Test { - [Fact] - public void doesPacmanDatabaseExist() { - string result = PacserverUtils.determinePacmanDatabaseDirectory(); - - Assert.Equivalent(result, "/var/lib/pacman/"); - } -} \ No newline at end of file diff --git a/src/Pacserver.Tests/readPacmanConfigTest.cs b/src/Pacserver.Tests/readPacmanConfigTest.cs new file mode 100644 index 0000000..90d5fd3 --- /dev/null +++ b/src/Pacserver.Tests/readPacmanConfigTest.cs @@ -0,0 +1,18 @@ +using Pacserver.Utils; + +namespace Pacserver.Tests; + +public class readPacmanConfigTest { + [Fact] + public void readPacmanConfig_returnsNoException() { + // Arrange + PacserverUtils utils = new PacserverUtils(); + + // Act + var exception = Record.Exception(() => utils.readPacmanConfig()); + + // Assert + Assert.Null(exception); + } +} + diff --git a/src/Pacserver/Pacserver.cs b/src/Pacserver/Pacserver.cs index 172f707..5fc9a72 100644 --- a/src/Pacserver/Pacserver.cs +++ b/src/Pacserver/Pacserver.cs @@ -3,69 +3,58 @@ using System.Text.RegularExpressions; namespace Pacserver.Utils; public class PacserverUtils { - public static string pacmanCacheDirectory { get; set; } = string.Empty; - public static string determinePacmanCacheDirectory() { - string defaultPacmanCacheDirectory = "/var/cache/pacman/pkg/"; - - Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 - string? line; - StreamReader file = new StreamReader("/etc/pacman.conf"); - while ((line = file.ReadLine()) is not null) { - if (line.Contains("CacheDir")) { - Match match = regex.Match(line); - if (match.Success) { - pacmanCacheDirectory = match.ToString(); - } else { - throw new Exception("Could not determine where pacman cache is! Would normally be found here " + defaultPacmanCacheDirectory); - } - } - } - file.Close(); - - return pacmanCacheDirectory; - } - + public string pacmanCacheDirectory { get; set; } = string.Empty; public static string pacmanDatabaseDirectory { get; set; } = string.Empty; - public static string determinePacmanDatabaseDirectory() { - string defaultPacmanDatabaseDirectory = "/var/lib/pacman/"; + public static List pathsToDetermine = new List() { "CacheDir", "DBPath" }; + public void readPacmanConfig() { + using (StreamReader file = new StreamReader("/etc/pacman.conf")) { + Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 + string? line; - Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 - string? line; - StreamReader file = new StreamReader("/etc/pacman.conf"); - while ((line = file.ReadLine()) is not null) { - if (line.Contains("DBPath")) { - Match match = regex.Match(line); - if (match.Success) { - pacmanDatabaseDirectory = match.ToString(); - } else { - throw new Exception("Could not determine where pacman database is! Would normally be found here " + defaultPacmanDatabaseDirectory); + while ((line = file.ReadLine()) is not null) { + foreach (string path in pathsToDetermine) { + if (line.Contains(path)) { + Match match = regex.Match(line); + if (match.Success) { + switch (path) { + case "CacheDir": + pacmanCacheDirectory = match.ToString(); + break; + case "DBPath": + pacmanDatabaseDirectory = match.ToString(); + break; + default: + throw new Exception("Could not deal with " + match.ToString()); + } + } else { + string pathsToDetermineString = string.Join(",", pathsToDetermine); + throw new Exception("Could not determine the necessary file paths: " + pathsToDetermineString); + } + } } } } - file.Close(); - - return pacmanDatabaseDirectory; } - public static void checkForNewerPackagesAndDatabases() { + public void checkForNewerPackagesAndDatabases() { } - private static List NewerPackagesAndDatabases = new List(); - public static async void TransferPacmanCache() { + private static List newerPackagesAndDatabases = new List(); + public async void transferPacmanCache() { HttpClient client = new HttpClient(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/"); MultipartFormDataContent content = new MultipartFormDataContent(); - foreach (String PkgOrDb in NewerPackagesAndDatabases) { - content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + PkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + PkgOrDb)); + foreach (string pkgOrDb in newerPackagesAndDatabases) { + content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + pkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + pkgOrDb)); } request.Content = content; await client.SendAsync(request); } - public static void transferPacmanDatabases() { + public void transferPacmanDatabases() { } } \ No newline at end of file diff --git a/src/Pacserver/Program.cs b/src/Pacserver/Program.cs index cab8ffd..9840ca8 100644 --- a/src/Pacserver/Program.cs +++ b/src/Pacserver/Program.cs @@ -12,7 +12,9 @@ public class Program { switch (args[0]) { case "determinePacmanCacheDirectory": - Console.WriteLine(PacserverUtils.determinePacmanCacheDirectory()); + PacserverUtils utils = new PacserverUtils(); + utils.readPacmanConfig(); + Console.WriteLine(utils.pacmanCacheDirectory); break; default: Console.ForegroundColor = ConsoleColor.Red;