Compare commits

...

5 Commits

Author SHA1 Message Date
f923628c53
Merge pull request 'feature/checkForNewerPackagesAndDatabases' (#2) from feature/checkForNewerPackagesAndDatabases into dev
Reviewed-on: #2
2023-06-18 15:40:02 +00:00
1994e92e9e
Minor changes && Add tests
Made a seperate method for writing the database access time files
Added tests for checkIfDatabasesWereModified
2023-06-18 17:24:56 +02:00
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
422909352e
Find new packages
We find new packages by getting the folder names in the DBPath local
2023-06-16 01:58:35 +02:00
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 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.");