1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-13 14:30:06 +02:00

🏗 WIP: Much improved Shell protocol

This commit is contained in:
2022-09-28 21:28:51 +02:00
parent 513a86ec39
commit bbac2632a2
6 changed files with 138 additions and 28 deletions

View File

@@ -8,20 +8,7 @@
import XCTest
class ShellTest: XCTestCase {
func test_default_shell_is_system_shell() {
XCTAssertTrue(Shell is SystemShell)
XCTAssertTrue(Shell.sync("php -v").output.contains("Copyright (c) The PHP Group"))
}
func test_system_shell_has_path() {
let systemShell = Shell as! SystemShell
XCTAssertTrue(systemShell.PATH.contains(":/usr/local/bin"))
XCTAssertTrue(systemShell.PATH.contains(":/usr/bin"))
}
class FakeShellTest: XCTestCase {
func test_we_can_predefine_responses_for_dummy_shell() {
let expectedPhpOutput = """
PHP 8.1.10 (cli) (built: Sep 3 2022 12:09:27) (NTS)

View File

@@ -0,0 +1,57 @@
//
// SystemShellTest.swift
// phpmon-tests
//
// Created by Nico Verbruggen on 28/09/2022.
// Copyright © 2022 Nico Verbruggen. All rights reserved.
//
import XCTest
class SystemShellTest: XCTestCase {
func test_system_shell_is_default() {
XCTAssertTrue(Shell is SystemShell)
XCTAssertTrue(Shell.sync("php -v").output.contains("Copyright (c) The PHP Group"))
}
func test_system_shell_has_path() {
let systemShell = Shell as! SystemShell
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.output)
},
withTimeout: 2.0
)
XCTAssertTrue(bits.contains("Hello world\n"))
XCTAssertTrue(bits.contains("Goodbye world"))
XCTAssertEqual("Hello world\nGoodbye world", shellOutput.output)
}
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()
}
wait(for: [expectation], timeout: 3.0)
}
}