|
| 1 | +import XCTest |
| 2 | +@testable import ntfy_macos |
| 3 | + |
| 4 | +final class ScriptRunnerTests: XCTestCase { |
| 5 | + var runner: ScriptRunner! |
| 6 | + var tempDir: URL! |
| 7 | + |
| 8 | + override func setUp() { |
| 9 | + super.setUp() |
| 10 | + runner = ScriptRunner() |
| 11 | + tempDir = FileManager.default.temporaryDirectory.appendingPathComponent("script-tests-\(UUID().uuidString)") |
| 12 | + try? FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true) |
| 13 | + } |
| 14 | + |
| 15 | + override func tearDown() { |
| 16 | + if let dir = tempDir { |
| 17 | + try? FileManager.default.removeItem(at: dir) |
| 18 | + } |
| 19 | + super.tearDown() |
| 20 | + } |
| 21 | + |
| 22 | + // MARK: - Script Validation |
| 23 | + |
| 24 | + func testValidateScriptNotFound() { |
| 25 | + let result = runner.validateScript(at: "/nonexistent/path/script.sh") |
| 26 | + XCTAssertFalse(result) |
| 27 | + } |
| 28 | + |
| 29 | + func testValidateScriptIsDirectory() { |
| 30 | + let result = runner.validateScript(at: tempDir.path) |
| 31 | + XCTAssertFalse(result) |
| 32 | + } |
| 33 | + |
| 34 | + func testValidateScriptNotExecutable() throws { |
| 35 | + let scriptPath = tempDir.appendingPathComponent("not-executable.sh") |
| 36 | + try "#!/bin/bash\necho hello".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 37 | + |
| 38 | + // File exists but is not executable |
| 39 | + let result = runner.validateScript(at: scriptPath.path) |
| 40 | + XCTAssertFalse(result) |
| 41 | + } |
| 42 | + |
| 43 | + func testValidateScriptExecutable() throws { |
| 44 | + let scriptPath = tempDir.appendingPathComponent("executable.sh") |
| 45 | + try "#!/bin/bash\necho hello".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 46 | + |
| 47 | + // Make it executable |
| 48 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 49 | + |
| 50 | + let result = runner.validateScript(at: scriptPath.path) |
| 51 | + XCTAssertTrue(result) |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - Script Execution |
| 55 | + |
| 56 | + func testRunScriptSynchronouslySimple() throws { |
| 57 | + let scriptPath = tempDir.appendingPathComponent("simple.sh") |
| 58 | + try "#!/bin/bash\necho 'Hello World'".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 59 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 60 | + |
| 61 | + let result = runner.runScriptSynchronously(at: scriptPath.path) |
| 62 | + |
| 63 | + XCTAssertEqual(result.exitCode, 0) |
| 64 | + XCTAssertTrue(result.output.contains("Hello World")) |
| 65 | + XCTAssertTrue(result.error.isEmpty) |
| 66 | + } |
| 67 | + |
| 68 | + func testRunScriptSynchronouslyWithArgument() throws { |
| 69 | + let scriptPath = tempDir.appendingPathComponent("with-arg.sh") |
| 70 | + try "#!/bin/bash\necho \"Received: $1\"".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 71 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 72 | + |
| 73 | + let result = runner.runScriptSynchronously(at: scriptPath.path, withArgument: "test-message") |
| 74 | + |
| 75 | + XCTAssertEqual(result.exitCode, 0) |
| 76 | + XCTAssertTrue(result.output.contains("Received: test-message")) |
| 77 | + } |
| 78 | + |
| 79 | + func testRunScriptSynchronouslyExitCode() throws { |
| 80 | + let scriptPath = tempDir.appendingPathComponent("exit-code.sh") |
| 81 | + try "#!/bin/bash\nexit 42".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 82 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 83 | + |
| 84 | + let result = runner.runScriptSynchronously(at: scriptPath.path) |
| 85 | + |
| 86 | + XCTAssertEqual(result.exitCode, 42) |
| 87 | + } |
| 88 | + |
| 89 | + func testRunScriptSynchronouslyStderr() throws { |
| 90 | + let scriptPath = tempDir.appendingPathComponent("stderr.sh") |
| 91 | + try "#!/bin/bash\necho 'Error message' >&2".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 92 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 93 | + |
| 94 | + let result = runner.runScriptSynchronously(at: scriptPath.path) |
| 95 | + |
| 96 | + XCTAssertEqual(result.exitCode, 0) |
| 97 | + XCTAssertTrue(result.error.contains("Error message")) |
| 98 | + } |
| 99 | + |
| 100 | + func testRunScriptSynchronouslyNotFound() { |
| 101 | + let result = runner.runScriptSynchronously(at: "/nonexistent/script.sh") |
| 102 | + |
| 103 | + XCTAssertEqual(result.exitCode, -1) |
| 104 | + XCTAssertTrue(result.error.contains("Failed to execute")) |
| 105 | + } |
| 106 | + |
| 107 | + func testRunScriptWithEnvironmentPath() throws { |
| 108 | + // Test that the enhanced PATH is set |
| 109 | + let scriptPath = tempDir.appendingPathComponent("check-path.sh") |
| 110 | + try "#!/bin/bash\necho $PATH".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 111 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 112 | + |
| 113 | + let result = runner.runScriptSynchronously(at: scriptPath.path) |
| 114 | + |
| 115 | + XCTAssertEqual(result.exitCode, 0) |
| 116 | + XCTAssertTrue(result.output.contains("/opt/homebrew/bin")) |
| 117 | + XCTAssertTrue(result.output.contains("/usr/local/bin")) |
| 118 | + } |
| 119 | + |
| 120 | + // MARK: - Special Characters |
| 121 | + |
| 122 | + func testRunScriptWithSpecialCharactersInArgument() throws { |
| 123 | + let scriptPath = tempDir.appendingPathComponent("special-chars.sh") |
| 124 | + try "#!/bin/bash\necho \"Arg: $1\"".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 125 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 126 | + |
| 127 | + let specialArg = "Hello 'World' with \"quotes\" and $pecial chars!" |
| 128 | + let result = runner.runScriptSynchronously(at: scriptPath.path, withArgument: specialArg) |
| 129 | + |
| 130 | + XCTAssertEqual(result.exitCode, 0) |
| 131 | + XCTAssertTrue(result.output.contains("Hello")) |
| 132 | + } |
| 133 | + |
| 134 | + func testRunScriptWithUnicodeArgument() throws { |
| 135 | + let scriptPath = tempDir.appendingPathComponent("unicode.sh") |
| 136 | + try "#!/bin/bash\necho \"Message: $1\"".write(to: scriptPath, atomically: true, encoding: .utf8) |
| 137 | + try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: scriptPath.path) |
| 138 | + |
| 139 | + let unicodeArg = "🔔 Notification: été, naïve, 日本語" |
| 140 | + let result = runner.runScriptSynchronously(at: scriptPath.path, withArgument: unicodeArg) |
| 141 | + |
| 142 | + XCTAssertEqual(result.exitCode, 0) |
| 143 | + XCTAssertTrue(result.output.contains("🔔")) |
| 144 | + } |
| 145 | +} |
0 commit comments