Tests/OrgProgramsTests/CommandLineToolTests.swift

19641366e5de9103a7d88bf80436ab2aebc03cf5
orgstar/Tests/OrgProgramsTests/CommandLineToolTests.swift

40 lines · 2474 bytes

 1import Foundation
 2import Testing
 3import OrgPrograms
 4
 5struct CommandLineToolTests {
 6    @Test func linksTheHelperAndReplacesALink() throws {
 7        let folder = try Folder()
 8        let app = folder.url.appendingPathComponent("orgstar.app")
 9        let helper = try folder.write("orgstar.app/Contents/Helpers/orgstar", "#!/bin/sh\n")
10        try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: helper.path)
11        #expect(CommandLineTool.helper(in: app) == helper)
12        let bin = folder.url.appendingPathComponent("bin")
13        try FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true)
14        try FileManager.default.createSymbolicLink(atPath: bin.appendingPathComponent("orgstar").path, withDestinationPath: "/old/orgstar")
15        let link = try CommandLineTool.install(helper, in: bin)
16        #expect(link == bin.appendingPathComponent("orgstar"))
17        #expect(try FileManager.default.destinationOfSymbolicLink(atPath: link.path) == helper.path)
18        #expect(try FileManager.default.contentsOfDirectory(atPath: bin.path) == ["orgstar"])
19    }
20
21    @Test func leavesOtherFilesAndReportsWhy() throws {
22        let folder = try Folder()
23        let helper = try folder.write("orgstar.app/Contents/Helpers/orgstar", "#!/bin/sh\n")
24        try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: helper.path)
25        let file = try folder.write("bin/orgstar", "mine")
26        #expect(throws: CommandLineTool.Failure.occupied(path: file.path)) { try CommandLineTool.install(helper, in: file.deletingLastPathComponent()) }
27        #expect(try String(contentsOf: file, encoding: .utf8) == "mine")
28        #expect(throws: CommandLineTool.Failure.missing) { try CommandLineTool.install(folder.url.appendingPathComponent("none"), in: folder.url) }
29        let locked = folder.url.appendingPathComponent("locked")
30        try FileManager.default.createDirectory(at: locked, withIntermediateDirectories: true)
31        try FileManager.default.setAttributes([.posixPermissions: 0o555], ofItemAtPath: locked.path)
32        defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: locked.path) }
33        #expect {
34            try CommandLineTool.install(helper, in: locked)
35        } throws: { error in
36            guard case .unwritable(let path, _) = error as? CommandLineTool.Failure else { return false }
37            return path == locked.path
38        }
39    }
40}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form