Compare commits

...

2 Commits

Author SHA1 Message Date
51aca7a2c2
Major advancements
Added a couple of test to check for acurate behavior in certain conditions
Added the capability to check if a database was modified
New commandline arguments
2023-06-17 02:47:35 +02:00
a1c1a0d5b0
Add fluentassertions test framework 2023-06-17 00:41:56 +02:00
5 changed files with 143 additions and 14 deletions

View File

@ -9,6 +9,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">

View File

@ -1 +1,2 @@
global using Xunit; global using Xunit;
global using FluentAssertions;

View File

@ -0,0 +1,60 @@
using Pacserver.Utils;
namespace Pacserver.Tests;
public class checkForNewerStuffTest {
[Fact]
public void checkForNewerPackages_throwsExceptionIfNoFilesExist() {
// Arrange
PacserverUtils utils = new PacserverUtils();
// Act
Action act = () => utils.diff("/tmp/before_update.txt", "/tmp/after_update.txt");
// Assert
act.Should().Throw<FileNotFoundException>().WithMessage("Necessary files could not be found");
}
[Fact]
public void getEveryPackageNameAndVersionViaFolderName_createsFiles() {
// Arrange
PacserverUtils utils = new PacserverUtils();
utils.readPacmanConfig();
// Act
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt");
// Assert
File.Exists("/tmp/before_update.txt").Should().BeTrue();
File.Exists("/tmp/after_update.txt").Should().BeTrue();
}
[Fact]
public void packageNamesAndVersion_notEmpty() {
// Arrange
PacserverUtils utils = new PacserverUtils();
utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
// Act
List<String> packageList = utils.packageNamesAndVersion;
// Assert
packageList.Should().NotBeNullOrEmpty();
}
[Fact]
public void getEveryPackageNameAndVersionViaFolderName_throwsExceptionIfListIsEmpty() {
// Arrange
PacserverUtils utils = new PacserverUtils();
Directory.CreateDirectory("/tmp/local");
utils.pacmanDatabaseDirectory = "/tmp/";
// Act
Action act = () => utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
// Assert
act.Should().Throw<Exception>().WithMessage("How did you execute this without any packages?");
}
}

View File

@ -4,7 +4,7 @@ using System.Text.RegularExpressions;
namespace Pacserver.Utils; namespace Pacserver.Utils;
public class PacserverUtils { public class PacserverUtils {
public string pacmanCacheDirectory = string.Empty; public string pacmanCacheDirectory = string.Empty;
public static string pacmanDatabaseDirectory = string.Empty; public string pacmanDatabaseDirectory = string.Empty;
public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" }; public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" };
public void readPacmanConfig() { public void readPacmanConfig() {
using (StreamReader file = new StreamReader("/etc/pacman.conf")) { using (StreamReader file = new StreamReader("/etc/pacman.conf")) {
@ -36,24 +36,77 @@ public class PacserverUtils {
} }
} }
private List<String> packageNamesAndVersion = new List<String>(); public List<String> packageNamesAndVersion = new List<String>();
public void getEveryPackageNameAndVersionViaFolderName(string filePath) { public void getEveryPackageNameAndVersionViaFolderName(string filePath) {
string[] directories = Directory.GetDirectories(pacmanDatabaseDirectory + "local/"); string[] directories = Directory.GetDirectories(pacmanDatabaseDirectory + "local/");
foreach (string directory in directories) { foreach (string directory in directories) {
packageNamesAndVersion.Add(new DirectoryInfo(directory).Name); packageNamesAndVersion.Add(new DirectoryInfo(directory).Name);
} }
File.WriteAllLines(filePath, packageNamesAndVersion);
if (packageNamesAndVersion.Capacity > 0) {
File.WriteAllLines(filePath, packageNamesAndVersion);
} else {
throw new Exception("How did you execute this without any packages?");
}
} }
public List<String> newerPackages = new List<String>(); public List<String> diffOfPackagesOrDatabases = new List<String>();
public void checkForNewerPackages() { public void diff(string before, string after) {
if (File.Exists("/tmp/before_update.txt") && File.Exists("/tmp/after_update.txt")) { if (File.Exists(before) && File.Exists(after)) {
newerPackages = File.ReadAllLines("/tmp/after_update.txt").Except(File.ReadLines("/tmp/before_update.txt")).ToList(); diffOfPackagesOrDatabases = File.ReadAllLines(after).Except(File.ReadLines(before)).ToList();
} else { } else {
throw new FileNotFoundException("Necessary files could not be found"); throw new FileNotFoundException("Necessary files could not be found");
} }
} }
public List<String> databases = new List<String>();
public void checkIfDatabasesWereModified(string mode, string filePath) {
string[] databases = Directory.GetFiles(pacmanDatabaseDirectory + "sync/");
// if (mode == "nuke") {
// using (File.Open(filePath, FileMode.Truncate, FileAccess.ReadWrite, FileShare.Delete)) {
// File.Delete(filePath);
// }
// }
foreach (string database in databases) {
switch (mode) {
case "before":
if (!File.Exists(filePath)) {
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (StreamWriter sw = new StreamWriter(filePath)) {
sw.WriteLine(database + " " + File.GetLastAccessTime(database));
}
}
} else if (File.Exists(filePath)) {
using (File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (var sw = new StreamWriter(filePath, true)) {
sw.WriteLine(database + " " + File.GetLastAccessTime(database));
}
}
}
break;
case "after":
if (!File.Exists(filePath)) {
using (File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (StreamWriter sw = new StreamWriter(filePath)) {
sw.WriteLine(database + " " + File.GetLastAccessTime(database));
}
}
} else if (File.Exists(filePath)) {
using (File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) {
using (var sw = new StreamWriter(filePath, true)) {
sw.WriteLine(database + " " + File.GetLastAccessTime(database));
}
}
}
break;
default:
throw new ArgumentException("No valid mode was selected");
}
}
}
private static List<String> newerPackagesAndDatabases = new List<String>(); private static List<String> newerPackagesAndDatabases = new List<String>();
public async void transferPacmanCache() { public async void transferPacmanCache() {
HttpClient client = new HttpClient(); HttpClient client = new HttpClient();

View File

@ -6,7 +6,7 @@ public class Program {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please specify an option."); Console.WriteLine("Please specify an option.");
Console.ResetColor(); Console.ResetColor();
Console.WriteLine("Possible options are: determinePacmanCacheDirectory"); Console.WriteLine("Possible options are: determinePacmanCacheDirectory, packagesBefore, packagesAfter, diffPackages, databasesBefore, databasesAfter, diffDatabases");
return; return;
} }
@ -17,17 +17,31 @@ public class Program {
utils.readPacmanConfig(); utils.readPacmanConfig();
Console.WriteLine(utils.pacmanCacheDirectory); Console.WriteLine(utils.pacmanCacheDirectory);
break; break;
case "before": case "packagesBefore":
utils.readPacmanConfig(); utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt"); utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
break; break;
case "after": case "packagesAfter":
utils.readPacmanConfig(); utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt"); utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt");
break; break;
case "diff": case "diffPackages":
utils.checkForNewerPackages(); utils.diff("/tmp/before_update.txt", "/tmp/after_update.txt");
Console.WriteLine(utils.newerPackages); string packages = string.Join(",", utils.diffOfPackagesOrDatabases);
Console.WriteLine(packages);
break;
case "databasesBefore":
utils.readPacmanConfig();
utils.checkIfDatabasesWereModified("before", "/tmp/databases_before.txt");
break;
case "databasesAfter":
utils.readPacmanConfig();
utils.checkIfDatabasesWereModified("after", "/tmp/databases_after.txt");
break;
case "diffDatabases":
utils.diff("/tmp/databases_before.txt", "/tmp/databases_after.txt");
string databases = string.Join(",", utils.diffOfPackagesOrDatabases);
Console.WriteLine(databases);
break; break;
default: default:
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;