From fa5c843619e67e495973f78e871b6c28b653c5fc Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 8 Nov 2022 20:14:10 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20RealFileSystem?= =?UTF-8?q?=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Common/Filesystem/RealFileSystem.swift | 4 +- phpmon/Common/Helpers/System.swift | 24 +++++-- .../Filesystem/RealFileSystemTest.swift | 70 ++++++++++++++++++- 3 files changed, 88 insertions(+), 10 deletions(-) diff --git a/phpmon/Common/Filesystem/RealFileSystem.swift b/phpmon/Common/Filesystem/RealFileSystem.swift index 7e8ef30..221ecd9 100644 --- a/phpmon/Common/Filesystem/RealFileSystem.swift +++ b/phpmon/Common/Filesystem/RealFileSystem.swift @@ -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 ) } diff --git a/phpmon/Common/Helpers/System.swift b/phpmon/Common/Helpers/System.swift index 9b8d000..92b8ba2 100644 --- a/phpmon/Common/Helpers/System.swift +++ b/phpmon/Common/Helpers/System.swift @@ -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 } diff --git a/tests/unit/Testables/Filesystem/RealFileSystemTest.swift b/tests/unit/Testables/Filesystem/RealFileSystemTest.swift index 349b1c0..ce52758 100644 --- a/tests/unit/Testables/Filesystem/RealFileSystemTest.swift +++ b/tests/unit/Testables/Filesystem/RealFileSystemTest.swift @@ -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)) + } + }