1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-03-27 14:30:08 +01:00

🚧 WIP: Test refactoring

This commit is contained in:
2025-10-09 16:21:46 +02:00
parent 9e76ca7b25
commit 8af304979b
3 changed files with 51 additions and 46 deletions

View File

@@ -9,39 +9,33 @@
import XCTest
class FeatureTestCase: XCTestCase {
var fakeFileSystem: TestableFileSystem {
let fs = ActiveFileSystem.shared
if fs is TestableFileSystem {
return fs as! TestableFileSystem
}
fatalError("The active filesystem is not a TestableFileSystem. Please use `ActiveFileSystem`.")
}
// TODO: make fake filesystem accessible via test case
public func assertFileSystemHas(
_ path: String,
file: StaticString = #filePath,
line: UInt = #line
line: UInt = #line,
fs: TestableFileSystem
) {
XCTAssertTrue(fakeFileSystem.files.keys.contains(path), file: file, line: line)
XCTAssertTrue(fs.files.keys.contains(path), file: file, line: line)
}
public func assertFileSystemDoesNotHave(
_ path: String,
file: StaticString = #filePath,
line: UInt = #line
line: UInt = #line,
fs: TestableFileSystem
) {
XCTAssertFalse(fakeFileSystem.files.keys.contains(path), file: file, line: line)
XCTAssertFalse(fs.files.keys.contains(path), file: file, line: line)
}
public func assertFileHasContents(
_ path: String,
contents: String,
file: StaticString = #filePath,
line: UInt = #line
line: UInt = #line,
fs: TestableFileSystem
) {
XCTAssertEqual(contents, fakeFileSystem.files[path]?.content, file: file, line: line)
XCTAssertEqual(contents, fs.files[path]?.content, file: file, line: line)
}
}

View File

@@ -11,8 +11,13 @@ import Foundation
@Suite(.serialized)
struct RealFileSystemTest {
var filesystem: FileSystemProtocol
init() throws {
ActiveFileSystem.useSystem()
let container = Container()
container.prepare()
filesystem = container.filesystem
}
private func createUniqueTemporaryDirectory() -> String {
@@ -26,7 +31,7 @@ struct RealFileSystemTest {
private func createTestBinaryFile(_ temporaryDirectory: String) -> String {
let executablePath = "\(temporaryDirectory)/exec.sh"
try! FileSystem.writeAtomicallyToFile(executablePath, content: """
try! filesystem.writeAtomicallyToFile(executablePath, content: """
!#/bin/bash
echo 'Hello world';
""")
@@ -35,18 +40,18 @@ struct RealFileSystemTest {
}
@Test func testable_fs_is_in_use() {
#expect(FileSystem is RealFileSystem)
#expect(filesystem is RealFileSystem)
}
@Test func temporary_path_exists() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
// True
#expect(FileSystem.directoryExists(temporaryDirectory))
#expect(FileSystem.anyExists(temporaryDirectory))
#expect(filesystem.directoryExists(temporaryDirectory))
#expect(filesystem.anyExists(temporaryDirectory))
// False
#expect(!FileSystem.fileExists(temporaryDirectory))
#expect(!filesystem.fileExists(temporaryDirectory))
}
@Test func directory_can_be_created_symlinked_and_read() {
@@ -54,18 +59,18 @@ struct RealFileSystemTest {
let folderPath = "\(temporaryDirectory)/brew/etc/lib/c"
try! FileSystem.createDirectory(folderPath, withIntermediateDirectories: true)
try! filesystem.createDirectory(folderPath, withIntermediateDirectories: true)
#expect(FileSystem.directoryExists("\(temporaryDirectory)/brew"))
#expect(FileSystem.directoryExists("\(temporaryDirectory)/brew/etc"))
#expect(FileSystem.directoryExists("\(temporaryDirectory)/brew/etc/lib"))
#expect(FileSystem.directoryExists("\(temporaryDirectory)/brew/etc/lib/c"))
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew"))
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew/etc"))
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew/etc/lib"))
#expect(filesystem.directoryExists("\(temporaryDirectory)/brew/etc/lib/c"))
_ = system("ln -s \(temporaryDirectory)/brew/etc/lib/c \(temporaryDirectory)/c")
#expect(FileSystem.directoryExists("\(temporaryDirectory)/c"))
#expect(FileSystem.isSymlink("\(temporaryDirectory)/c"))
#expect(filesystem.directoryExists("\(temporaryDirectory)/c"))
#expect(filesystem.isSymlink("\(temporaryDirectory)/c"))
#expect(
try! FileSystem.getDestinationOfSymlink("\(temporaryDirectory)/c") ==
try! filesystem.getDestinationOfSymlink("\(temporaryDirectory)/c") ==
"\(temporaryDirectory)/brew/etc/lib/c"
)
@@ -78,7 +83,7 @@ struct RealFileSystemTest {
let executable = self.createTestBinaryFile(temporaryDirectory)
#expect(
try! FileSystem.getStringFromFile(executable) ==
try! filesystem.getStringFromFile(executable) ==
"""
!#/bin/bash
echo 'Hello world';
@@ -90,45 +95,45 @@ struct RealFileSystemTest {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
#expect(FileSystem.isWriteableFile(executable))
#expect(!FileSystem.isExecutableFile(executable))
#expect(filesystem.isWriteableFile(executable))
#expect(!filesystem.isExecutableFile(executable))
try! FileSystem.makeExecutable(executable)
try! filesystem.makeExecutable(executable)
#expect(FileSystem.isExecutableFile(executable))
#expect(!FileSystem.isDirectory(executable))
#expect(!FileSystem.isSymlink(executable))
#expect(filesystem.isExecutableFile(executable))
#expect(!filesystem.isDirectory(executable))
#expect(!filesystem.isSymlink(executable))
}
@Test func non_existent_file_is_not_symlink_or_directory() {
let path = "/path/that/does/not/exist"
#expect(!FileSystem.isDirectory(path))
#expect(!FileSystem.isSymlink(path))
#expect(!filesystem.isDirectory(path))
#expect(!filesystem.isSymlink(path))
}
@Test func moving_file() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
#expect(FileSystem.fileExists(executable))
#expect(filesystem.fileExists(executable))
let newExecutable = executable.replacingOccurrences(of: "/exec.sh", with: "/file.txt")
try! FileSystem.move(from: executable, to: newExecutable)
try! filesystem.move(from: executable, to: newExecutable)
#expect(FileSystem.fileExists(newExecutable))
#expect(!FileSystem.fileExists(executable))
#expect(filesystem.fileExists(newExecutable))
#expect(!filesystem.fileExists(executable))
}
@Test func deleting_file() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
#expect(FileSystem.fileExists(executable))
#expect(filesystem.fileExists(executable))
try! FileSystem.remove(executable)
try! filesystem.remove(executable)
#expect(!FileSystem.fileExists(executable))
#expect(!filesystem.fileExists(executable))
}
}