Minor changes && Add tests
Made a seperate method for writing the database access time files Added tests for checkIfDatabasesWereModified
This commit is contained in:
parent
51aca7a2c2
commit
1994e92e9e
31
src/Pacserver.Tests/checkIfDatabasesWereModifiedTest.cs
Normal file
31
src/Pacserver.Tests/checkIfDatabasesWereModifiedTest.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -63,46 +63,32 @@ public class PacserverUtils {
|
|||||||
public void checkIfDatabasesWereModified(string mode, string filePath) {
|
public void checkIfDatabasesWereModified(string mode, string filePath) {
|
||||||
string[] databases = Directory.GetFiles(pacmanDatabaseDirectory + "sync/");
|
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) {
|
foreach (string database in databases) {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case "before":
|
case "before":
|
||||||
if (!File.Exists(filePath)) {
|
writeDatabaseAccessTimeToFile(filePath, database);
|
||||||
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;
|
break;
|
||||||
case "after":
|
case "after":
|
||||||
if (!File.Exists(filePath)) {
|
writeDatabaseAccessTimeToFile(filePath, database);
|
||||||
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;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ArgumentException("No valid mode was selected");
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user