Merge pull request 'feature/checkForNewerPackagesAndDatabases' (#2) from feature/checkForNewerPackagesAndDatabases into dev

Reviewed-on: #2
This commit is contained in:
ProfessionalUwU 2023-06-18 15:40:02 +00:00 committed by Gitea
commit f923628c53
Signed by: Gitea
GPG Key ID: FCACD432A6DD0FB5
6 changed files with 179 additions and 6 deletions

View File

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

View File

@ -1 +1,2 @@
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

@ -0,0 +1,31 @@
using Pacserver.Utils;
namespace Pacserver.Tests;
public class checkIfDatabasesWereModifiedTest {
[Fact]
public void checkIfDatabasesWereModified_throwsExceptionIfNoValidModeIsGiven() {
// Arrange
PacserverUtils utils = new PacserverUtils();
utils.readPacmanConfig();
// Act
Action act = () => utils.checkIfDatabasesWereModified("test", "/tmp/test.txt");
// Assert
act.Should().Throw<ArgumentException>().WithMessage("No valid mode was given. Valid modes are before and after");
}
[Fact]
public void checkIfDatabasesWereModified_throwsNoExceptionIfValidModeIsGiven() {
// Arrange
PacserverUtils utils = new PacserverUtils();
utils.readPacmanConfig();
// Act
Action act = () => utils.checkIfDatabasesWereModified("before", "/tmp/test.txt");
// Assert
act.Should().NotThrow();
}
}

View File

@ -3,8 +3,8 @@ using System.Text.RegularExpressions;
namespace Pacserver.Utils;
public class PacserverUtils {
public string pacmanCacheDirectory { get; set; } = string.Empty;
public static string pacmanDatabaseDirectory { get; set; } = string.Empty;
public string pacmanCacheDirectory = string.Empty;
public string pacmanDatabaseDirectory = string.Empty;
public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" };
public void readPacmanConfig() {
using (StreamReader file = new StreamReader("/etc/pacman.conf")) {
@ -36,8 +36,61 @@ public class PacserverUtils {
}
}
public void checkForNewerPackagesAndDatabases() {
public List<String> packageNamesAndVersion = new List<String>();
public void getEveryPackageNameAndVersionViaFolderName(string filePath) {
string[] directories = Directory.GetDirectories(pacmanDatabaseDirectory + "local/");
foreach (string directory in directories) {
packageNamesAndVersion.Add(new DirectoryInfo(directory).Name);
}
if (packageNamesAndVersion.Capacity > 0) {
File.WriteAllLines(filePath, packageNamesAndVersion);
} else {
throw new Exception("How did you execute this without any packages?");
}
}
public List<String> diffOfPackagesOrDatabases = new List<String>();
public void diff(string before, string after) {
if (File.Exists(before) && File.Exists(after)) {
diffOfPackagesOrDatabases = File.ReadAllLines(after).Except(File.ReadLines(before)).ToList();
} else {
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/");
foreach (string database in databases) {
switch (mode) {
case "before":
writeDatabaseAccessTimeToFile(filePath, database);
break;
case "after":
writeDatabaseAccessTimeToFile(filePath, database);
break;
default:
throw new ArgumentException("No valid mode was given. Valid modes are before and after");
}
}
}
public void writeDatabaseAccessTimeToFile(string filePath, string database) {
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));
}
}
}
}
private static List<String> newerPackagesAndDatabases = new List<String>();

View File

@ -6,16 +6,43 @@ public class Program {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please specify an option.");
Console.ResetColor();
Console.WriteLine("Possible options are: determinePacmanCacheDirectory");
Console.WriteLine("Possible options are: determinePacmanCacheDirectory, packagesBefore, packagesAfter, diffPackages, databasesBefore, databasesAfter, diffDatabases");
return;
}
PacserverUtils utils = new PacserverUtils();
switch (args[0]) {
case "determinePacmanCacheDirectory":
PacserverUtils utils = new PacserverUtils();
utils.readPacmanConfig();
Console.WriteLine(utils.pacmanCacheDirectory);
break;
case "packagesBefore":
utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/before_update.txt");
break;
case "packagesAfter":
utils.readPacmanConfig();
utils.getEveryPackageNameAndVersionViaFolderName("/tmp/after_update.txt");
break;
case "diffPackages":
utils.diff("/tmp/before_update.txt", "/tmp/after_update.txt");
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;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(args[0] + " is not a recognized option.");