Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import Darwinimport Foundationimport Testing
@testable import ParcelApp
@Suitestruct AppStandardStreamsTests { @Test func redirectsBothStreamsToOneAppendOnlyLog() throws { try withDirectory { directory in let log = directory.appendingPathComponent("app log") try Data("existing\n".utf8).write(to: log) let output = try FileHandle(forWritingTo: directory.appendingPathComponent("output")) let errors = try FileHandle(forWritingTo: directory.appendingPathComponent("errors")) defer { close(output, errors) } try AppStandardStreams.redirect( environment: ["PARCEL_E2E": "1", "PARCEL_E2E_LOG_PATH": log.path], outputDescriptor: output.fileDescriptor, errorDescriptor: errors.fileDescriptor) try output.write(contentsOf: Data("stdout\n".utf8)) try errors.write(contentsOf: Data("stderr\n".utf8)) #expect(try String(contentsOf: log, encoding: .utf8) == "existing\nstdout\nstderr\n") } }
@Test func createsAPrivateLogWhenThePathDoesNotExist() throws { try withDirectory { directory in let log = directory.appendingPathComponent("new log") let output = try FileHandle(forWritingTo: directory.appendingPathComponent("output")) let errors = try FileHandle(forWritingTo: directory.appendingPathComponent("errors")) defer { close(output, errors) } try AppStandardStreams.redirect( environment: ["PARCEL_E2E": "1", "PARCEL_E2E_LOG_PATH": log.path], outputDescriptor: output.fileDescriptor, errorDescriptor: errors.fileDescriptor) let attributes = try FileManager.default.attributesOfItem(atPath: log.path) let permissions = try #require(attributes[.posixPermissions] as? NSNumber) #expect(permissions.intValue & 0o777 == 0o600) } }
@Test(arguments: [ [:], ["PARCEL_E2E": "1"], ["PARCEL_E2E_LOG_PATH": "relative"], ["PARCEL_E2E": "0", "PARCEL_E2E_LOG_PATH": "relative"], ]) func onlyExplicitE2ELoggingChangesDescriptors(environment: [String: String]) throws { try AppStandardStreams.redirect(environment: environment, outputDescriptor: -1, errorDescriptor: -1) }
@Test(arguments: ["", "relative", "/tmp/null\0suffix"]) func rejectsInvalidPaths(path: String) { #expect(throws: CocoaError.self) { try AppStandardStreams.redirect(environment: ["PARCEL_E2E": "1", "PARCEL_E2E_LOG_PATH": path]) } }
@Test func rejectsSymlinksHardLinksDirectoriesAndMissingParents() throws { try withDirectory { directory in let output = try FileHandle(forWritingTo: directory.appendingPathComponent("output")) let errors = try FileHandle(forWritingTo: directory.appendingPathComponent("errors")) defer { close(output, errors) } let target = directory.appendingPathComponent("target") let link = directory.appendingPathComponent("symlink") let hardLink = directory.appendingPathComponent("hardlink") try Data("unchanged".utf8).write(to: target) try FileManager.default.createSymbolicLink(at: link, withDestinationURL: target) try FileManager.default.linkItem(at: target, to: hardLink) for path in [ link.path, hardLink.path, directory.path, directory.appendingPathComponent("missing/log").path, ] { #expect(throws: (any Error).self) { try AppStandardStreams.redirect( environment: ["PARCEL_E2E": "1", "PARCEL_E2E_LOG_PATH": path], outputDescriptor: output.fileDescriptor, errorDescriptor: errors.fileDescriptor) } } #expect(try String(contentsOf: target, encoding: .utf8) == "unchanged") } }
@Test func descriptorFailureIsReported() throws { try withDirectory { directory in #expect(throws: NSError.self) { try AppStandardStreams.redirect( environment: [ "PARCEL_E2E": "1", "PARCEL_E2E_LOG_PATH": directory.appendingPathComponent("log").path, ], outputDescriptor: -1, errorDescriptor: -1) } } }
private func withDirectory(_ body: (URL) throws -> Void) throws { let directory = FileManager.default.temporaryDirectory.appendingPathComponent("streams-\(UUID())") try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: false) defer { do { try FileManager.default.removeItem(at: directory) } catch { Issue.record(error) } } try Data().write(to: directory.appendingPathComponent("output")) try Data().write(to: directory.appendingPathComponent("errors")) try body(directory) }
private func close(_ output: FileHandle, _ errors: FileHandle) { do { try output.close() try errors.close() } catch { Issue.record(error) } }}