1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 03:50:08 +02:00

Add tests for RealFileSystem class

This commit is contained in:
2022-11-08 20:14:10 +01:00
parent bc739e1982
commit fa5c843619
3 changed files with 88 additions and 10 deletions

View File

@ -9,13 +9,79 @@
import XCTest
class RealFileSystemTest: XCTestCase {
override class func setUp() {
override func setUp() {
ActiveFileSystem.useSystem()
}
private func createUniqueTemporaryDirectory() -> String {
let tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true)
let fullTempDirectoryPath = tempDirectoryURL.appendingPathComponent("phpmon-fs-tests").path
try? FileManager.default.removeItem(atPath: fullTempDirectoryPath)
try! FileManager.default.createDirectory(atPath: fullTempDirectoryPath, withIntermediateDirectories: false)
return fullTempDirectoryPath
}
func test_testable_fs_is_in_use() {
XCTAssertTrue(FileSystem is RealFileSystem)
}
func test_temporary_path_exists() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
// True
XCTAssertTrue(FileSystem.directoryExists(temporaryDirectory))
XCTAssertTrue(FileSystem.anyExists(temporaryDirectory))
// False
XCTAssertFalse(FileSystem.fileExists(temporaryDirectory))
}
private func createTestBinaryFile(_ temporaryDirectory: String) -> String {
let executablePath = "\(temporaryDirectory)/exec.sh"
try! FileSystem.writeAtomicallyToFile(executablePath, content: """
!#/bin/bash
echo 'Hello world';
""")
return executablePath
}
func test_make_binary_executable() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
XCTAssertTrue(FileSystem.isWriteableFile(executable))
XCTAssertFalse(FileSystem.isExecutableFile(executable))
try! FileSystem.makeExecutable(executable)
XCTAssertTrue(FileSystem.isExecutableFile(executable))
}
func test_moving_file() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
XCTAssertTrue(FileSystem.fileExists(executable))
let newExecutable = executable.replacingOccurrences(of: "/exec.sh", with: "/file.txt")
try! FileSystem.move(from: executable, to: newExecutable)
XCTAssertTrue(FileSystem.fileExists(newExecutable))
XCTAssertFalse(FileSystem.fileExists(executable))
}
func test_deleting_file() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
XCTAssertTrue(FileSystem.fileExists(executable))
try! FileSystem.remove(executable)
XCTAssertFalse(FileSystem.fileExists(executable))
}
}