mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-05 04:20:06 +01:00
♻️ Migrate more tests to Swift Testing (3/?)
This commit is contained in:
@@ -6,10 +6,12 @@
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
class RealFileSystemTest: XCTestCase {
|
||||
override func setUp() {
|
||||
@Suite(.serialized)
|
||||
struct RealFileSystemTest {
|
||||
init() throws {
|
||||
ActiveFileSystem.useSystem()
|
||||
}
|
||||
|
||||
@@ -21,45 +23,6 @@ class RealFileSystemTest: XCTestCase {
|
||||
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))
|
||||
}
|
||||
|
||||
func test_directory_can_be_created_symlinked_and_read() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
|
||||
let folderPath = "\(temporaryDirectory)/brew/etc/lib/c"
|
||||
|
||||
try! FileSystem.createDirectory(folderPath, withIntermediateDirectories: true)
|
||||
|
||||
XCTAssertTrue(FileSystem.directoryExists("\(temporaryDirectory)/brew"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("\(temporaryDirectory)/brew/etc"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("\(temporaryDirectory)/brew/etc/lib"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("\(temporaryDirectory)/brew/etc/lib/c"))
|
||||
|
||||
_ = system("ln -s \(temporaryDirectory)/brew/etc/lib/c \(temporaryDirectory)/c")
|
||||
XCTAssertTrue(FileSystem.directoryExists("\(temporaryDirectory)/c"))
|
||||
XCTAssertTrue(FileSystem.isSymlink("\(temporaryDirectory)/c"))
|
||||
XCTAssertEqual(
|
||||
try! FileSystem.getDestinationOfSymlink("\(temporaryDirectory)/c"),
|
||||
"\(temporaryDirectory)/brew/etc/lib/c"
|
||||
)
|
||||
|
||||
let contents = try! FileSystem.getShallowContentsOfDirectory("\(temporaryDirectory)/brew/etc/lib/c")
|
||||
XCTAssertEqual([], contents)
|
||||
}
|
||||
|
||||
private func createTestBinaryFile(_ temporaryDirectory: String) -> String {
|
||||
let executablePath = "\(temporaryDirectory)/exec.sh"
|
||||
|
||||
@@ -71,12 +34,51 @@ class RealFileSystemTest: XCTestCase {
|
||||
return executablePath
|
||||
}
|
||||
|
||||
func test_can_read_file_as_text() {
|
||||
@Test func testable_fs_is_in_use() {
|
||||
#expect(FileSystem is RealFileSystem)
|
||||
}
|
||||
|
||||
@Test func temporary_path_exists() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
|
||||
// True
|
||||
#expect(FileSystem.directoryExists(temporaryDirectory))
|
||||
#expect(FileSystem.anyExists(temporaryDirectory))
|
||||
|
||||
// False
|
||||
#expect(!FileSystem.fileExists(temporaryDirectory))
|
||||
}
|
||||
|
||||
@Test func directory_can_be_created_symlinked_and_read() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
|
||||
let folderPath = "\(temporaryDirectory)/brew/etc/lib/c"
|
||||
|
||||
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"))
|
||||
|
||||
_ = system("ln -s \(temporaryDirectory)/brew/etc/lib/c \(temporaryDirectory)/c")
|
||||
#expect(FileSystem.directoryExists("\(temporaryDirectory)/c"))
|
||||
#expect(FileSystem.isSymlink("\(temporaryDirectory)/c"))
|
||||
#expect(
|
||||
try! FileSystem.getDestinationOfSymlink("\(temporaryDirectory)/c") ==
|
||||
"\(temporaryDirectory)/brew/etc/lib/c"
|
||||
)
|
||||
|
||||
let contents = try! FileSystem.getShallowContentsOfDirectory("\(temporaryDirectory)/brew/etc/lib/c")
|
||||
#expect([] == contents)
|
||||
}
|
||||
|
||||
@Test func can_read_file_as_text() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
XCTAssertEqual(
|
||||
try! FileSystem.getStringFromFile(executable),
|
||||
#expect(
|
||||
try! FileSystem.getStringFromFile(executable) ==
|
||||
"""
|
||||
!#/bin/bash
|
||||
echo 'Hello world';
|
||||
@@ -84,50 +86,49 @@ class RealFileSystemTest: XCTestCase {
|
||||
)
|
||||
}
|
||||
|
||||
func test_make_binary_executable() {
|
||||
@Test func make_binary_executable() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
XCTAssertTrue(FileSystem.isWriteableFile(executable))
|
||||
XCTAssertFalse(FileSystem.isExecutableFile(executable))
|
||||
#expect(FileSystem.isWriteableFile(executable))
|
||||
#expect(!FileSystem.isExecutableFile(executable))
|
||||
|
||||
try! FileSystem.makeExecutable(executable)
|
||||
|
||||
XCTAssertTrue(FileSystem.isExecutableFile(executable))
|
||||
XCTAssertFalse(FileSystem.isDirectory(executable))
|
||||
XCTAssertFalse(FileSystem.isSymlink(executable))
|
||||
#expect(FileSystem.isExecutableFile(executable))
|
||||
#expect(!FileSystem.isDirectory(executable))
|
||||
#expect(!FileSystem.isSymlink(executable))
|
||||
}
|
||||
|
||||
func test_non_existent_file_is_not_symlink_or_directory() {
|
||||
@Test func non_existent_file_is_not_symlink_or_directory() {
|
||||
let path = "/path/that/does/not/exist"
|
||||
|
||||
XCTAssertFalse(FileSystem.isDirectory(path))
|
||||
XCTAssertFalse(FileSystem.isSymlink(path))
|
||||
#expect(!FileSystem.isDirectory(path))
|
||||
#expect(!FileSystem.isSymlink(path))
|
||||
}
|
||||
|
||||
func test_moving_file() {
|
||||
@Test func moving_file() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
XCTAssertTrue(FileSystem.fileExists(executable))
|
||||
#expect(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))
|
||||
#expect(FileSystem.fileExists(newExecutable))
|
||||
#expect(!FileSystem.fileExists(executable))
|
||||
}
|
||||
|
||||
func test_deleting_file() {
|
||||
@Test func deleting_file() {
|
||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||
|
||||
XCTAssertTrue(FileSystem.fileExists(executable))
|
||||
#expect(FileSystem.fileExists(executable))
|
||||
|
||||
try! FileSystem.remove(executable)
|
||||
|
||||
XCTAssertFalse(FileSystem.fileExists(executable))
|
||||
#expect(!FileSystem.fileExists(executable))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
//
|
||||
// TestableFileSystemTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 01/11/2022.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
@Suite(.serialized)
|
||||
struct TestableFileSystemTest {
|
||||
init() throws {
|
||||
ActiveFileSystem.useTestable([
|
||||
"/home/user/bin/foo": .fake(.binary),
|
||||
"/home/user/docs": .fake(.symlink, "/home/user/documents"),
|
||||
"/home/user/documents/script.sh": .fake(.text, "echo 'cool';"),
|
||||
"/home/user/documents/nice.txt": .fake(.text, "69"),
|
||||
"/home/user/documents/filters/filter1.txt": .fake(.text, "F1"),
|
||||
"/home/user/documents/filters/filter2.txt": .fake(.text, "F2")
|
||||
])
|
||||
}
|
||||
|
||||
@Test func testable_fs_is_in_use() {
|
||||
#expect(FileSystem is TestableFileSystem)
|
||||
}
|
||||
|
||||
@Test func intermediate_directories_are_automatically_created() {
|
||||
#expect(FileSystem.directoryExists("/"))
|
||||
#expect(FileSystem.directoryExists("/home"))
|
||||
#expect(FileSystem.directoryExists("/home/user"))
|
||||
#expect(FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(FileSystem.directoryExists("/home/user/bin"))
|
||||
}
|
||||
|
||||
@Test func binary_directory_is_writable() {
|
||||
#expect(FileSystem.isWriteableFile("/home/user/bin"))
|
||||
}
|
||||
|
||||
@Test func binary_exists() {
|
||||
#expect(FileSystem.isExecutableFile("/home/user/bin/foo"))
|
||||
}
|
||||
|
||||
@Test func can_write_text_to_executable() throws {
|
||||
try! FileSystem.writeAtomicallyToFile("/home/user/bin/bar", content: "bar bar bar!")
|
||||
|
||||
#expect(FileSystem.fileExists("/home/user/bin/bar"))
|
||||
#expect(!FileSystem.isExecutableFile("/home/user/bin/bar"))
|
||||
|
||||
try! FileSystem.makeExecutable("/home/user/bin/bar")
|
||||
|
||||
#expect(FileSystem.isExecutableFile("/home/user/bin/bar"))
|
||||
}
|
||||
|
||||
@Test func can_create_directory() throws {
|
||||
try! FileSystem.createDirectory(
|
||||
"/home/nico/phpmon/config",
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
|
||||
#expect(FileSystem
|
||||
.anyExists("/home/nico/phpmon/config"))
|
||||
#expect(FileSystem.directoryExists("/home/nico/phpmon/config"))
|
||||
}
|
||||
|
||||
@Test func can_create_nested_directories() throws {
|
||||
try FileSystem.createDirectory(
|
||||
"/home/user/thing/epic/nested/directories",
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
|
||||
#expect(FileSystem.directoryExists("/"))
|
||||
#expect(FileSystem.directoryExists("/home"))
|
||||
#expect(FileSystem.directoryExists("/home/user"))
|
||||
#expect(FileSystem.directoryExists("/home/user/thing"))
|
||||
#expect(FileSystem.directoryExists("/home/user/thing/epic/nested"))
|
||||
#expect(FileSystem.directoryExists("/home/user/thing/epic/nested/directories"))
|
||||
}
|
||||
|
||||
@Test func can_list_directory_contents() throws {
|
||||
let contents = try! FileSystem.getShallowContentsOfDirectory("/home/user/documents")
|
||||
|
||||
#expect(
|
||||
contents.sorted() ==
|
||||
[
|
||||
"script.sh",
|
||||
"nice.txt",
|
||||
"filters"
|
||||
].sorted()
|
||||
)
|
||||
}
|
||||
|
||||
@Test func can_delete_directory_recursively() {
|
||||
#expect(FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
#expect(FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
|
||||
try! FileSystem.remove("/home/user/documents")
|
||||
|
||||
#expect(!FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(!FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
#expect(!FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
}
|
||||
|
||||
@Test func can_move_directory() {
|
||||
#expect(FileSystem.directoryExists("/home/user/documents"))
|
||||
#expect(FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
#expect(FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
|
||||
try! FileSystem.move(from: "/home/user/documents", to: "/home/user/new")
|
||||
|
||||
#expect(FileSystem.directoryExists("/home/user/new"))
|
||||
#expect(FileSystem.directoryExists("/home/user/new/filters"))
|
||||
#expect(FileSystem.fileExists("/home/user/new/filters/filter1.txt"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
//
|
||||
// RealShellTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 28/09/2022.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
@Suite(.serialized)
|
||||
struct RealShellTest {
|
||||
|
||||
init() async throws {
|
||||
// Reset to the default shell
|
||||
ActiveShell.useSystem()
|
||||
}
|
||||
|
||||
@Test func system_shell_is_default() async {
|
||||
#expect(Shell is RealShell)
|
||||
|
||||
let output = await Shell.pipe("php -v")
|
||||
|
||||
#expect(output.out.contains("Copyright (c) The PHP Group"))
|
||||
}
|
||||
|
||||
@Test func system_shell_can_be_used_synchronously() {
|
||||
#expect(Shell is RealShell)
|
||||
|
||||
let output = Shell.sync("php -v")
|
||||
|
||||
#expect(output.out.contains("Copyright (c) The PHP Group"))
|
||||
}
|
||||
|
||||
@Test func system_shell_has_path() {
|
||||
let systemShell = Shell as! RealShell
|
||||
|
||||
#expect(systemShell.PATH.contains(":/usr/local/bin"))
|
||||
#expect(systemShell.PATH.contains(":/usr/bin"))
|
||||
}
|
||||
|
||||
@Test func system_shell_can_buffer_output() async {
|
||||
var bits: [String] = []
|
||||
|
||||
let (_, shellOutput) = try! await Shell.attach(
|
||||
"php -r \"echo 'Hello world' . PHP_EOL; usleep(200); echo 'Goodbye world';\"",
|
||||
didReceiveOutput: { incoming, _ in
|
||||
bits.append(incoming)
|
||||
},
|
||||
withTimeout: 2.0
|
||||
)
|
||||
|
||||
#expect(bits.contains("Hello world\n"))
|
||||
#expect(bits.contains("Goodbye world"))
|
||||
#expect("Hello world\nGoodbye world" == shellOutput.out)
|
||||
}
|
||||
|
||||
@Test func system_shell_can_timeout_and_throw_error() async {
|
||||
await #expect(throws: ShellError.timedOut) {
|
||||
try await Shell.attach(
|
||||
"php -r \"sleep(1);\"",
|
||||
didReceiveOutput: { _, _ in },
|
||||
withTimeout: .seconds(0.1)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func can_run_multiple_shell_commands_in_parallel() async throws {
|
||||
let start = ContinuousClock.now
|
||||
|
||||
await withTaskGroup(of: Void.self) { group in
|
||||
group.addTask { await Shell.quiet("php -r \"usleep(700000);\"") }
|
||||
group.addTask { await Shell.quiet("php -r \"usleep(700000);\"") }
|
||||
group.addTask { await Shell.quiet("php -r \"usleep(700000);\"") }
|
||||
}
|
||||
|
||||
let duration = start.duration(to: .now)
|
||||
#expect(duration < .milliseconds(1500)) // Should complete in ~700ms if parallel
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,12 @@
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
class TestableShellTest: XCTestCase {
|
||||
func test_fake_shell_output_can_be_declared() async {
|
||||
@Suite(.serialized)
|
||||
struct TestableShellTest {
|
||||
@Test func fake_shell_output_can_be_declared() async {
|
||||
let greeting = BatchFakeShellOutput(items: [
|
||||
.instant("Hello world\n"),
|
||||
.delayed(0.3, "Goodbye world")
|
||||
@@ -17,10 +19,10 @@ class TestableShellTest: XCTestCase {
|
||||
|
||||
let output = await greeting.outputInstantaneously()
|
||||
|
||||
XCTAssertEqual("Hello world\nGoodbye world", output.out)
|
||||
#expect("Hello world\nGoodbye world" == output.out)
|
||||
}
|
||||
|
||||
func test_fake_shell_can_output_in_realtime() async {
|
||||
@Test func fake_shell_can_output_in_realtime() async {
|
||||
let greeting = BatchFakeShellOutput(items: [
|
||||
.instant("Hello world\n"),
|
||||
.delayed(2, "Goodbye world")
|
||||
@@ -28,10 +30,10 @@ class TestableShellTest: XCTestCase {
|
||||
|
||||
let output = await greeting.output(didReceiveOutput: { _, _ in })
|
||||
|
||||
XCTAssertEqual("Hello world\nGoodbye world", output.out)
|
||||
#expect("Hello world\nGoodbye world" == output.out)
|
||||
}
|
||||
|
||||
func test_fake_shell_synchronous_output() {
|
||||
@Test func fake_shell_synchronous_output() {
|
||||
let greeting = BatchFakeShellOutput(items: [
|
||||
.instant("Hello world\n"),
|
||||
.delayed(0.2, "Goodbye world")
|
||||
@@ -39,10 +41,10 @@ class TestableShellTest: XCTestCase {
|
||||
|
||||
let output = greeting.syncOutput()
|
||||
|
||||
XCTAssertEqual("Hello world\nGoodbye world", output.out)
|
||||
#expect("Hello world\nGoodbye world" == output.out)
|
||||
}
|
||||
|
||||
func test_fake_shell_usage() {
|
||||
@Test func fake_shell_usage() {
|
||||
let expectedOutput = """
|
||||
PHP 8.3.0 (cli) (built: Nov 21 2023 14:40:35) (NTS)
|
||||
Copyright (c) The PHP Group
|
||||
@@ -56,13 +58,13 @@ class TestableShellTest: XCTestCase {
|
||||
"echo $PATH": .instant("/Users/user/bin:/opt/homebrew/bin")
|
||||
])
|
||||
|
||||
XCTAssertEqual(expectedOutput, shell.sync("php -v").out)
|
||||
XCTAssertEqual("/Users/user/bin:/opt/homebrew/bin", shell.sync("echo $PATH").out)
|
||||
#expect(expectedOutput == shell.sync("php -v").out)
|
||||
#expect("/Users/user/bin:/opt/homebrew/bin" == shell.sync("echo $PATH").out)
|
||||
}
|
||||
|
||||
func test_fake_shell_has_path() {
|
||||
@Test func fake_shell_has_path() {
|
||||
ActiveShell.useTestable([:])
|
||||
|
||||
XCTAssertEqual(Shell.PATH, "/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin")
|
||||
#expect(Shell.PATH == "/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin")
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,11 @@
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import Testing
|
||||
import Foundation
|
||||
|
||||
class TestableConfigurationTest: XCTestCase {
|
||||
func test_configuration_can_be_saved_as_json() async {
|
||||
struct TestableConfigurationTest {
|
||||
@Test func configuration_can_be_saved_as_json() async {
|
||||
// WORKING
|
||||
var configuration = TestableConfigurations.working
|
||||
|
||||
@@ -36,5 +37,10 @@ class TestableConfigurationTest: XCTestCase {
|
||||
atomically: true,
|
||||
encoding: .utf8
|
||||
)
|
||||
|
||||
// Verify that the files were written to disk
|
||||
#expect(FileSystem.fileExists(NSHomeDirectory() + "/.phpmon_fconf_working.json"))
|
||||
#expect(FileSystem.fileExists(NSHomeDirectory() + "/.phpmon_fconf_working_no_valet.json"))
|
||||
#expect(FileSystem.fileExists(NSHomeDirectory() + "/.phpmon_fconf_broken.json"))
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
//
|
||||
// TestableFileSystemTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 01/11/2022.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
class TestableFileSystemTest: XCTestCase {
|
||||
|
||||
override func setUp() async throws {
|
||||
ActiveFileSystem.useTestable([
|
||||
"/home/user/bin/foo": .fake(.binary),
|
||||
"/home/user/docs": .fake(.symlink, "/home/user/documents"),
|
||||
"/home/user/documents/script.sh": .fake(.text, "echo 'cool';"),
|
||||
"/home/user/documents/nice.txt": .fake(.text, "69"),
|
||||
"/home/user/documents/filters/filter1.txt": .fake(.text, "F1"),
|
||||
"/home/user/documents/filters/filter2.txt": .fake(.text, "F2")
|
||||
])
|
||||
}
|
||||
|
||||
func test_testable_fs_is_in_use() {
|
||||
XCTAssertTrue(FileSystem is TestableFileSystem)
|
||||
}
|
||||
|
||||
func test_intermediate_directories_are_automatically_created() {
|
||||
XCTAssertTrue(FileSystem.directoryExists("/"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/documents"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/bin"))
|
||||
}
|
||||
|
||||
func test_binary_directory_is_writable() {
|
||||
XCTAssertTrue(FileSystem.isWriteableFile("/home/user/bin"))
|
||||
}
|
||||
|
||||
func test_binary_exists() {
|
||||
XCTAssertTrue(FileSystem.isExecutableFile("/home/user/bin/foo"))
|
||||
}
|
||||
|
||||
func test_can_write_text_to_executable() throws {
|
||||
try! FileSystem.writeAtomicallyToFile("/home/user/bin/bar", content: "bar bar bar!")
|
||||
|
||||
XCTAssertFalse(FileSystem.isExecutableFile("/home/user/bin/bar"))
|
||||
|
||||
try! FileSystem.makeExecutable("/home/user/bin/bar")
|
||||
|
||||
XCTAssertTrue(FileSystem.isExecutableFile("/home/user/bin/bar"))
|
||||
}
|
||||
|
||||
func test_can_create_directory() throws {
|
||||
try! FileSystem.createDirectory(
|
||||
"/home/nico/phpmon/config",
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
|
||||
XCTAssertTrue(FileSystem
|
||||
.anyExists("/home/nico/phpmon/config"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/nico/phpmon/config"))
|
||||
}
|
||||
|
||||
func test_can_create_nested_directories() throws {
|
||||
try FileSystem.createDirectory(
|
||||
"/home/user/thing/epic/nested/directories",
|
||||
withIntermediateDirectories: true
|
||||
)
|
||||
|
||||
XCTAssertTrue(FileSystem.directoryExists("/"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/thing"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/thing/epic/nested"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/thing/epic/nested/directories"))
|
||||
}
|
||||
|
||||
func test_can_list_directory_contents() throws {
|
||||
let contents = try! FileSystem.getShallowContentsOfDirectory("/home/user/documents")
|
||||
|
||||
XCTAssertEqual(
|
||||
contents.sorted(),
|
||||
[
|
||||
"script.sh",
|
||||
"nice.txt",
|
||||
"filters"
|
||||
].sorted()
|
||||
)
|
||||
}
|
||||
|
||||
func test_can_delete_directory_recursively() {
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/documents"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
XCTAssertTrue(FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
|
||||
try! FileSystem.remove("/home/user/documents")
|
||||
|
||||
XCTAssertFalse(FileSystem.directoryExists("/home/user/documents"))
|
||||
XCTAssertFalse(FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
XCTAssertFalse(FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
}
|
||||
|
||||
func test_can_move_directory() {
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/documents"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/documents/filters"))
|
||||
XCTAssertTrue(FileSystem.fileExists("/home/user/documents/filters/filter1.txt"))
|
||||
|
||||
try! FileSystem.move(from: "/home/user/documents", to: "/home/user/new")
|
||||
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/new"))
|
||||
XCTAssertTrue(FileSystem.directoryExists("/home/user/new/filters"))
|
||||
XCTAssertTrue(FileSystem.fileExists("/home/user/new/filters/filter1.txt"))
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
//
|
||||
// RealShellTest.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 28/09/2022.
|
||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
class RealShellTest: XCTestCase {
|
||||
|
||||
override func setUp() async throws {
|
||||
// Reset to the default shell
|
||||
ActiveShell.useSystem()
|
||||
}
|
||||
|
||||
func test_system_shell_is_default() async {
|
||||
XCTAssertTrue(Shell is RealShell)
|
||||
|
||||
let output = await Shell.pipe("php -v")
|
||||
|
||||
XCTAssertTrue(output.out.contains("Copyright (c) The PHP Group"))
|
||||
}
|
||||
|
||||
func test_system_shell_can_be_used_synchronously() {
|
||||
XCTAssertTrue(Shell is RealShell)
|
||||
|
||||
let output = Shell.sync("php -v")
|
||||
|
||||
XCTAssertTrue(output.out.contains("Copyright (c) The PHP Group"))
|
||||
}
|
||||
|
||||
func test_system_shell_has_path() {
|
||||
let systemShell = Shell as! RealShell
|
||||
|
||||
XCTAssertTrue(systemShell.PATH.contains(":/usr/local/bin"))
|
||||
XCTAssertTrue(systemShell.PATH.contains(":/usr/bin"))
|
||||
}
|
||||
|
||||
func test_system_shell_can_buffer_output() async {
|
||||
var bits: [String] = []
|
||||
|
||||
let (_, shellOutput) = try! await Shell.attach(
|
||||
"php -r \"echo 'Hello world' . PHP_EOL; usleep(200); echo 'Goodbye world';\"",
|
||||
didReceiveOutput: { incoming, _ in
|
||||
bits.append(incoming)
|
||||
},
|
||||
withTimeout: 2.0
|
||||
)
|
||||
|
||||
XCTAssertTrue(bits.contains("Hello world\n"))
|
||||
XCTAssertTrue(bits.contains("Goodbye world"))
|
||||
XCTAssertEqual("Hello world\nGoodbye world", shellOutput.out)
|
||||
}
|
||||
|
||||
func test_system_shell_can_timeout_and_throw_error() async {
|
||||
let expectation = XCTestExpectation(description: #function)
|
||||
|
||||
do {
|
||||
_ = try await Shell.attach(
|
||||
"php -r \"sleep(1);\"",
|
||||
didReceiveOutput: { _, _ in },
|
||||
withTimeout: 0.1
|
||||
)
|
||||
} catch {
|
||||
XCTAssertEqual(error as? ShellError, ShellError.timedOut)
|
||||
expectation.fulfill()
|
||||
}
|
||||
|
||||
await fulfillment(of: [expectation], timeout: 5.0)
|
||||
}
|
||||
|
||||
func test_system_processes_run_in_parallel() async {
|
||||
let expectation = XCTestExpectation(description: #function)
|
||||
|
||||
let thing = {
|
||||
await Shell.quiet("php -r \"usleep(700);\"")
|
||||
await Shell.quiet("php -r \"usleep(700);\"")
|
||||
await Shell.quiet("php -r \"usleep(700);\"")
|
||||
expectation.fulfill()
|
||||
}
|
||||
|
||||
await thing()
|
||||
await fulfillment(of: [expectation], timeout: 5.0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user