mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-12-21 11:10:08 +01:00
♻️ Migrate more tests to Swift Testing (3/?)
This commit is contained in:
@@ -1346,15 +1346,16 @@
|
|||||||
path = Integration;
|
path = Integration;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
036C3A222E5CBC33008DAEDF /* _ST */ = {
|
036C3A222E5CBC33008DAEDF /* SwiftTestMigrated */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
039C291E2E8AA39B007F5FAB /* Api */,
|
039C291E2E8AA39B007F5FAB /* Api */,
|
||||||
C4C1019927C65A4D001FACC2 /* Commands */,
|
C4C1019927C65A4D001FACC2 /* Commands */,
|
||||||
036C3A232E5CBC57008DAEDF /* Parsers */,
|
|
||||||
036C39062E5C8890008DAEDF /* Integration */,
|
036C39062E5C8890008DAEDF /* Integration */,
|
||||||
|
036C3A232E5CBC57008DAEDF /* Parsers */,
|
||||||
|
03D53E902E8AE089001B1671 /* Testables */,
|
||||||
);
|
);
|
||||||
path = _ST;
|
path = SwiftTestMigrated;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
036C3A232E5CBC57008DAEDF /* Parsers */ = {
|
036C3A232E5CBC57008DAEDF /* Parsers */ = {
|
||||||
@@ -1408,6 +1409,16 @@
|
|||||||
path = Provision;
|
path = Provision;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
|
03D53E902E8AE089001B1671 /* Testables */ = {
|
||||||
|
isa = PBXGroup;
|
||||||
|
children = (
|
||||||
|
C4E2E84628FC1D8C003B070C /* TestableConfigurationTest.swift */,
|
||||||
|
C413E43328DA3E8F00AE33C7 /* Shell */,
|
||||||
|
C471E6DA28F9AFCB0021E251 /* Filesystem */,
|
||||||
|
);
|
||||||
|
path = Testables;
|
||||||
|
sourceTree = "<group>";
|
||||||
|
};
|
||||||
5420395726135DB800FB00FA /* Preferences */ = {
|
5420395726135DB800FB00FA /* Preferences */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
@@ -1935,16 +1946,6 @@
|
|||||||
path = "Domain List";
|
path = "Domain List";
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
};
|
};
|
||||||
C471E6D928F9AFC20021E251 /* Testables */ = {
|
|
||||||
isa = PBXGroup;
|
|
||||||
children = (
|
|
||||||
C471E6DA28F9AFCB0021E251 /* Filesystem */,
|
|
||||||
C413E43328DA3E8F00AE33C7 /* Shell */,
|
|
||||||
C4E2E84628FC1D8C003B070C /* TestableConfigurationTest.swift */,
|
|
||||||
);
|
|
||||||
path = Testables;
|
|
||||||
sourceTree = "<group>";
|
|
||||||
};
|
|
||||||
C471E6DA28F9AFCB0021E251 /* Filesystem */ = {
|
C471E6DA28F9AFCB0021E251 /* Filesystem */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
@@ -2325,9 +2326,8 @@
|
|||||||
C4F7807A25D7F84B000DBC97 /* unit */ = {
|
C4F7807A25D7F84B000DBC97 /* unit */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
036C3A222E5CBC33008DAEDF /* _ST */,
|
|
||||||
C471E6D928F9AFC20021E251 /* Testables */,
|
|
||||||
C40C7F1C27720E1400DDDCDC /* Test Files */,
|
C40C7F1C27720E1400DDDCDC /* Test Files */,
|
||||||
|
036C3A222E5CBC33008DAEDF /* SwiftTestMigrated */,
|
||||||
C4C1019827C65A1A001FACC2 /* Versions */,
|
C4C1019827C65A1A001FACC2 /* Versions */,
|
||||||
);
|
);
|
||||||
path = unit;
|
path = unit;
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ class TestableFileSystem: FileSystemProtocol {
|
|||||||
let adjustedKey = key.contains("~") ? key.replacingOccurrences(of: "~", with: self.homeDirectory) : key
|
let adjustedKey = key.contains("~") ? key.replacingOccurrences(of: "~", with: self.homeDirectory) : key
|
||||||
self.files[adjustedKey] = value
|
self.files[adjustedKey] = value
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure that intermediate directories are created
|
// Ensure that intermediate directories are created
|
||||||
for file in self.files {
|
for file in self.files {
|
||||||
self.createIntermediateDirectories(file.key)
|
self.createIntermediateDirectories(file.key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,12 @@
|
|||||||
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
import XCTest
|
import Testing
|
||||||
|
import Foundation
|
||||||
|
|
||||||
class RealFileSystemTest: XCTestCase {
|
@Suite(.serialized)
|
||||||
override func setUp() {
|
struct RealFileSystemTest {
|
||||||
|
init() throws {
|
||||||
ActiveFileSystem.useSystem()
|
ActiveFileSystem.useSystem()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,45 +23,6 @@ class RealFileSystemTest: XCTestCase {
|
|||||||
return fullTempDirectoryPath
|
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 {
|
private func createTestBinaryFile(_ temporaryDirectory: String) -> String {
|
||||||
let executablePath = "\(temporaryDirectory)/exec.sh"
|
let executablePath = "\(temporaryDirectory)/exec.sh"
|
||||||
|
|
||||||
@@ -71,12 +34,51 @@ class RealFileSystemTest: XCTestCase {
|
|||||||
return executablePath
|
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 temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||||
|
|
||||||
XCTAssertEqual(
|
#expect(
|
||||||
try! FileSystem.getStringFromFile(executable),
|
try! FileSystem.getStringFromFile(executable) ==
|
||||||
"""
|
"""
|
||||||
!#/bin/bash
|
!#/bin/bash
|
||||||
echo 'Hello world';
|
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 temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||||
|
|
||||||
XCTAssertTrue(FileSystem.isWriteableFile(executable))
|
#expect(FileSystem.isWriteableFile(executable))
|
||||||
XCTAssertFalse(FileSystem.isExecutableFile(executable))
|
#expect(!FileSystem.isExecutableFile(executable))
|
||||||
|
|
||||||
try! FileSystem.makeExecutable(executable)
|
try! FileSystem.makeExecutable(executable)
|
||||||
|
|
||||||
XCTAssertTrue(FileSystem.isExecutableFile(executable))
|
#expect(FileSystem.isExecutableFile(executable))
|
||||||
XCTAssertFalse(FileSystem.isDirectory(executable))
|
#expect(!FileSystem.isDirectory(executable))
|
||||||
XCTAssertFalse(FileSystem.isSymlink(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"
|
let path = "/path/that/does/not/exist"
|
||||||
|
|
||||||
XCTAssertFalse(FileSystem.isDirectory(path))
|
#expect(!FileSystem.isDirectory(path))
|
||||||
XCTAssertFalse(FileSystem.isSymlink(path))
|
#expect(!FileSystem.isSymlink(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_moving_file() {
|
@Test func moving_file() {
|
||||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||||
|
|
||||||
XCTAssertTrue(FileSystem.fileExists(executable))
|
#expect(FileSystem.fileExists(executable))
|
||||||
|
|
||||||
let newExecutable = executable.replacingOccurrences(of: "/exec.sh", with: "/file.txt")
|
let newExecutable = executable.replacingOccurrences(of: "/exec.sh", with: "/file.txt")
|
||||||
|
|
||||||
try! FileSystem.move(from: executable, to: newExecutable)
|
try! FileSystem.move(from: executable, to: newExecutable)
|
||||||
|
|
||||||
XCTAssertTrue(FileSystem.fileExists(newExecutable))
|
#expect(FileSystem.fileExists(newExecutable))
|
||||||
XCTAssertFalse(FileSystem.fileExists(executable))
|
#expect(!FileSystem.fileExists(executable))
|
||||||
}
|
}
|
||||||
|
|
||||||
func test_deleting_file() {
|
@Test func deleting_file() {
|
||||||
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
let temporaryDirectory = self.createUniqueTemporaryDirectory()
|
||||||
let executable = self.createTestBinaryFile(temporaryDirectory)
|
let executable = self.createTestBinaryFile(temporaryDirectory)
|
||||||
|
|
||||||
XCTAssertTrue(FileSystem.fileExists(executable))
|
#expect(FileSystem.fileExists(executable))
|
||||||
|
|
||||||
try! FileSystem.remove(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.
|
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
import XCTest
|
import Testing
|
||||||
|
import Foundation
|
||||||
|
|
||||||
class TestableShellTest: XCTestCase {
|
@Suite(.serialized)
|
||||||
func test_fake_shell_output_can_be_declared() async {
|
struct TestableShellTest {
|
||||||
|
@Test func fake_shell_output_can_be_declared() async {
|
||||||
let greeting = BatchFakeShellOutput(items: [
|
let greeting = BatchFakeShellOutput(items: [
|
||||||
.instant("Hello world\n"),
|
.instant("Hello world\n"),
|
||||||
.delayed(0.3, "Goodbye world")
|
.delayed(0.3, "Goodbye world")
|
||||||
@@ -17,10 +19,10 @@ class TestableShellTest: XCTestCase {
|
|||||||
|
|
||||||
let output = await greeting.outputInstantaneously()
|
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: [
|
let greeting = BatchFakeShellOutput(items: [
|
||||||
.instant("Hello world\n"),
|
.instant("Hello world\n"),
|
||||||
.delayed(2, "Goodbye world")
|
.delayed(2, "Goodbye world")
|
||||||
@@ -28,10 +30,10 @@ class TestableShellTest: XCTestCase {
|
|||||||
|
|
||||||
let output = await greeting.output(didReceiveOutput: { _, _ in })
|
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: [
|
let greeting = BatchFakeShellOutput(items: [
|
||||||
.instant("Hello world\n"),
|
.instant("Hello world\n"),
|
||||||
.delayed(0.2, "Goodbye world")
|
.delayed(0.2, "Goodbye world")
|
||||||
@@ -39,10 +41,10 @@ class TestableShellTest: XCTestCase {
|
|||||||
|
|
||||||
let output = greeting.syncOutput()
|
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 = """
|
let expectedOutput = """
|
||||||
PHP 8.3.0 (cli) (built: Nov 21 2023 14:40:35) (NTS)
|
PHP 8.3.0 (cli) (built: Nov 21 2023 14:40:35) (NTS)
|
||||||
Copyright (c) The PHP Group
|
Copyright (c) The PHP Group
|
||||||
@@ -56,13 +58,13 @@ class TestableShellTest: XCTestCase {
|
|||||||
"echo $PATH": .instant("/Users/user/bin:/opt/homebrew/bin")
|
"echo $PATH": .instant("/Users/user/bin:/opt/homebrew/bin")
|
||||||
])
|
])
|
||||||
|
|
||||||
XCTAssertEqual(expectedOutput, shell.sync("php -v").out)
|
#expect(expectedOutput == shell.sync("php -v").out)
|
||||||
XCTAssertEqual("/Users/user/bin:/opt/homebrew/bin", shell.sync("echo $PATH").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([:])
|
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.
|
// Copyright © 2023 Nico Verbruggen. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
import XCTest
|
import Testing
|
||||||
|
import Foundation
|
||||||
|
|
||||||
class TestableConfigurationTest: XCTestCase {
|
struct TestableConfigurationTest {
|
||||||
func test_configuration_can_be_saved_as_json() async {
|
@Test func configuration_can_be_saved_as_json() async {
|
||||||
// WORKING
|
// WORKING
|
||||||
var configuration = TestableConfigurations.working
|
var configuration = TestableConfigurations.working
|
||||||
|
|
||||||
@@ -36,5 +37,10 @@ class TestableConfigurationTest: XCTestCase {
|
|||||||
atomically: true,
|
atomically: true,
|
||||||
encoding: .utf8
|
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