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:
@ -61,7 +61,7 @@ class RealFileSystem: FileSystemProtocol {
|
||||
// MARK: — FS Attributes
|
||||
|
||||
func makeExecutable(_ path: String) throws {
|
||||
system("chmod +x \(path.replacingTildeWithHomeDirectory)")
|
||||
_ = system("chmod +x \(path.replacingTildeWithHomeDirectory)")
|
||||
}
|
||||
|
||||
// MARK: - Checks
|
||||
@ -69,6 +69,8 @@ class RealFileSystem: FileSystemProtocol {
|
||||
func isExecutableFile(_ path: String) -> Bool {
|
||||
return FileManager.default.isExecutableFile(
|
||||
atPath: path.replacingTildeWithHomeDirectory
|
||||
) && FileManager.default.isReadableFile(
|
||||
atPath: path.replacingTildeWithHomeDirectory
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -8,11 +8,21 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public func system(_ command: String) {
|
||||
let argsArray = command.split(separator: " ").map { String($0) }
|
||||
guard argsArray.isEmpty else { return }
|
||||
let command = strdup(argsArray.first!)
|
||||
let args = argsArray.map { strdup($0) } + [nil]
|
||||
posix_spawn(nil, command, nil, nil, args, nil)
|
||||
return
|
||||
/**
|
||||
Run a simple blocking Shell command on the user's own system.
|
||||
Avoid using this method in favor of the fakeable Shell class unless needed for express system operations.
|
||||
*/
|
||||
public func system(_ command: String) -> String {
|
||||
let task = Process()
|
||||
task.launchPath = "/bin/sh"
|
||||
task.arguments = ["-c", command]
|
||||
|
||||
let pipe = Pipe()
|
||||
task.standardOutput = pipe
|
||||
task.launch()
|
||||
|
||||
let data = pipe.fileHandleForReading.readDataToEndOfFile()
|
||||
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
|
||||
|
||||
return output
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user