1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2026-04-01 09:10:08 +02:00

♻️ Refactor tests to Swift Testing

This commit is contained in:
2025-10-16 15:57:41 +02:00
parent 378a8a5547
commit fa403b5a99
24 changed files with 191 additions and 203 deletions

View File

@@ -0,0 +1,139 @@
//
// RealFileSystemTest.swift
// Unit Tests
//
// Created by Nico Verbruggen on 02/11/2022.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Testing
import Foundation
@Suite(.serialized)
struct RealFileSystemTest {
var filesystem: FileSystemProtocol
init() throws {
let container = Container()
container.prepare()
filesystem = container.filesystem
}
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
}
private func createTestBinaryFile(_ temporaryDirectory: String) -> String {
let executablePath = "\(temporaryDirectory)/exec.sh"
try! filesystem.writeAtomicallyToFile(executablePath, content: """
!#/bin/bash
echo 'Hello world';
""")
return executablePath
}
@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)
#expect(
try! filesystem.getStringFromFile(executable) ==
"""
!#/bin/bash
echo 'Hello world';
"""
)
}
@Test func make_binary_executable() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
#expect(filesystem.isWriteableFile(executable))
#expect(!filesystem.isExecutableFile(executable))
try! filesystem.makeExecutable(executable)
#expect(filesystem.isExecutableFile(executable))
#expect(!filesystem.isDirectory(executable))
#expect(!filesystem.isSymlink(executable))
}
@Test func non_existent_file_is_not_symlink_or_directory() {
let path = "/path/that/does/not/exist"
#expect(!filesystem.isDirectory(path))
#expect(!filesystem.isSymlink(path))
}
@Test func moving_file() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
#expect(filesystem.fileExists(executable))
let newExecutable = executable.replacingOccurrences(of: "/exec.sh", with: "/file.txt")
try! filesystem.move(from: executable, to: newExecutable)
#expect(filesystem.fileExists(newExecutable))
#expect(!filesystem.fileExists(executable))
}
@Test func deleting_file() {
let temporaryDirectory = self.createUniqueTemporaryDirectory()
let executable = self.createTestBinaryFile(temporaryDirectory)
#expect(filesystem.fileExists(executable))
try! filesystem.remove(executable)
#expect(!filesystem.fileExists(executable))
}
}

View File

@@ -0,0 +1,122 @@
//
// 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 {
var container: Container
init() throws {
container = Container.fake(files: [
"/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")
])
}
var FileSystem: FileSystemProtocol {
return container.filesystem
}
@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"))
}
}

View File

@@ -0,0 +1,80 @@
//
// 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 {
var container: Container
init() async throws {
// Reset to the default shell
container = Container.real()
}
@Test func system_shell_is_default() async {
#expect(container.shell is RealShell)
let output = await container.shell.pipe("php -v")
#expect(output.out.contains("Copyright (c) The PHP Group"))
}
@Test func system_shell_can_be_used_synchronously() {
#expect(container.shell is RealShell)
let output = container.shell.sync("php -v")
#expect(output.out.contains("Copyright (c) The PHP Group"))
}
@Test func system_shell_has_path() {
let systemShell = container.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 container.shell.attach(
"php -r \"echo 'Hello world' . PHP_EOL; usleep(500); echo 'Goodbye world';\"",
didReceiveOutput: { incoming, _ in
bits.append(incoming)
},
withTimeout: 2.0
)
#expect("Hello world\nGoodbye world" == shellOutput.out)
}
@Test func system_shell_can_timeout_and_throw_error() async {
await #expect(throws: ShellError.timedOut) {
try await container.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 container.shell.quiet("php -r \"usleep(700000);\"") }
group.addTask { await container.shell.quiet("php -r \"usleep(700000);\"") }
group.addTask { await container.shell.quiet("php -r \"usleep(700000);\"") }
}
let duration = start.duration(to: .now)
#expect(duration < .milliseconds(1500)) // Should complete in ~700ms if parallel
}
}

View File

@@ -0,0 +1,70 @@
//
// TestableShellTest.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 20/09/2022.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Testing
import Foundation
@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")
])
let output = await greeting.outputInstantaneously()
#expect("Hello world\nGoodbye world" == output.out)
}
@Test func fake_shell_can_output_in_realtime() async {
let greeting = BatchFakeShellOutput(items: [
.instant("Hello world\n"),
.delayed(2, "Goodbye world")
])
let output = await greeting.output(didReceiveOutput: { _, _ in })
#expect("Hello world\nGoodbye world" == output.out)
}
@Test func fake_shell_synchronous_output() {
let greeting = BatchFakeShellOutput(items: [
.instant("Hello world\n"),
.delayed(0.2, "Goodbye world")
])
let output = greeting.syncOutput()
#expect("Hello world\nGoodbye world" == output.out)
}
@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
Zend Engine v4.3.0, Copyright (c) Zend Technologies
with Xdebug v3.2.2, Copyright (c) 2002-2023, by Derick Rethans
with Zend OPcache v8.3.0, Copyright (c), by Zend Technologies
"""
let shell = TestableShell(expectations: [
"php -v": .instant(expectedOutput),
"echo $PATH": .instant("/Users/user/bin:/opt/homebrew/bin")
])
#expect(expectedOutput == shell.sync("php -v").out)
#expect("/Users/user/bin:/opt/homebrew/bin" == shell.sync("echo $PATH").out)
}
@Test func fake_shell_has_path() {
let container = Container.fake()
#expect(container.shell.PATH == "/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin")
}
}

View File

@@ -0,0 +1,48 @@
//
// TestableConfigurationTest.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 16/10/2022.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Testing
import Foundation
struct TestableConfigurationTest {
@Test func configuration_can_be_saved_as_json() async {
let container = Container.real()
// WORKING
var configuration = TestableConfigurations.working
try! configuration.toJson().write(
toFile: NSHomeDirectory() + "/.phpmon_fconf_working.json",
atomically: true,
encoding: .utf8
)
// WORKING (WITHOUT VALET)
let valetFreeConfiguration = TestableConfigurations.workingWithoutValet
try! valetFreeConfiguration.toJson().write(
toFile: NSHomeDirectory() + "/.phpmon_fconf_working_no_valet.json",
atomically: true,
encoding: .utf8
)
// NOT WORKING
configuration.filesystem["/opt/homebrew/bin/php"] = nil
try! configuration.toJson().write(
toFile: NSHomeDirectory() + "/.phpmon_fconf_broken.json",
atomically: true,
encoding: .utf8
)
// Verify that the files were written to disk
#expect(container.filesystem.fileExists(NSHomeDirectory() + "/.phpmon_fconf_working.json"))
#expect(container.filesystem.fileExists(NSHomeDirectory() + "/.phpmon_fconf_working_no_valet.json"))
#expect(container.filesystem.fileExists(NSHomeDirectory() + "/.phpmon_fconf_broken.json"))
}
}