codeburn/mac/Tests/CodeBurnMenubarTests/CodeburnCLIPathTests.swift
Resham Joshi ee5cf7c38f
Some checks are pending
CI / semgrep (push) Waiting to run
macOS Menubar CI / test (push) Waiting to run
Tests / desktop (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.15.0) (push) Waiting to run
Tests / test (24) (push) Waiting to run
Tests / test (26) (push) Waiting to run
Tests / vscode (push) Waiting to run
Windows Menubar CI / check (ubuntu-latest) (push) Waiting to run
Windows Menubar CI / check (windows-latest) (push) Waiting to run
fix(menubar): find the desktop app's bundled CLI when no CLI is on PATH (#1871)
With only the desktop app installed from the Homebrew cask, the menu bar
failed to load Today with "nonZeroExit (code: 127, stderr: env: codeburn:
No such file or directory)". The menu bar looked for a codeburn in the
path recorded for it and in package-manager folders, then fell back to the
bare name. A desktop-only machine has nothing in those folders, so when the
record was missing or no longer ran, nothing was found and the error named
nothing.

The packaged desktop app now carries a plain launcher at
CodeBurn.app/Contents/Resources/cli/codeburn that runs its bundled CLI
through its own executable and finds its own bundle. The menu bar checks
it, in /Applications and ~/Applications, after the recorded path and the
package-manager folders. When none of them runs, Today says which paths
were tried. The CLI's menubar installer records the same launcher instead
of refusing the install when nothing named codeburn is on PATH.
2026-11-05 05:59:42 +01:00

271 lines
13 KiB
Swift

import Foundation
import Testing
@testable import CodeBurnMenubar
@Suite("CodeburnCLI PATH")
struct CodeburnCLIPathTests {
@Test("Spotlight-minimal PATH can launch a mise npm-backend CLI")
func spotlightCanLaunchMiseCLI() throws {
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("CodeburnCLIPathTests-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: root) }
let home = root.appendingPathComponent("home", isDirectory: true)
let wrapper = home
.appendingPathComponent(".local/share/mise/installs/npm-codeburn/latest/node_modules/.bin/codeburn")
let nodeShim = home.appendingPathComponent(".local/share/mise/shims/node")
try FileManager.default.createDirectory(
at: wrapper.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try FileManager.default.createDirectory(
at: nodeShim.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try "#!/bin/sh\nexec node \"$@\"\n".write(to: wrapper, atomically: true, encoding: .utf8)
try "#!/bin/sh\nprintf 'mise-node-ok\\n'\n".write(to: nodeShim, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: wrapper.path)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: nodeShim.path)
let augmentedPath = CodeburnCLI.augmentedPath(
"/usr/bin:/bin",
homeDirectory: home.path,
environment: [:]
)
// Keep the behavior fixture independent of tools installed on the CI host.
// Every retained entry came from the production augmentation above.
let isolatedPath = augmentedPath
.split(separator: ":")
.map(String.init)
.filter { $0 == "/usr/bin" || $0 == "/bin" || $0.hasPrefix(home.path + "/") }
.joined(separator: ":")
#expect(isolatedPath.split(separator: ":").contains(Substring(nodeShim.deletingLastPathComponent().path)))
let process = Process()
let stdout = Pipe()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = ["--", wrapper.path, "--version"]
process.environment = [
"HOME": home.path,
"PATH": isolatedPath,
]
process.standardOutput = stdout
process.standardError = Pipe()
try process.run()
process.waitUntilExit()
let output = String(decoding: stdout.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
#expect(process.terminationStatus == 0)
#expect(output == "mise-node-ok\n")
}
@Test("custom mise data directory is added once")
func customMiseDataDirectoryIsDeduplicated() {
let customShims = "/Volumes/Tools/mise/shims"
let path = CodeburnCLI.augmentedPath(
"/usr/bin:\(customShims)",
homeDirectory: "/Users/test",
environment: ["MISE_DATA_DIR": "/Volumes/Tools/mise"]
)
#expect(path.split(separator: ":").filter { $0 == Substring(customShims) }.count == 1)
}
@Test("Spotlight-minimal PATH can launch a CLI whose only node is a Nix profile")
func spotlightCanLaunchNixCLI() throws {
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("CodeburnCLIPathTests-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: root) }
let home = root.appendingPathComponent("home", isDirectory: true)
let wrapper = home.appendingPathComponent(".local/bin/codeburn")
let nodeBin = home.appendingPathComponent(".nix-profile/bin/node")
try FileManager.default.createDirectory(
at: wrapper.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try FileManager.default.createDirectory(
at: nodeBin.deletingLastPathComponent(),
withIntermediateDirectories: true
)
// Mirrors the real shim, which resolves `node` through PATH via `#!/usr/bin/env node`.
try "#!/bin/sh\nexec node \"$@\"\n".write(to: wrapper, atomically: true, encoding: .utf8)
try "#!/bin/sh\nprintf 'nix-node-ok\\n'\n".write(to: nodeBin, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: wrapper.path)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: nodeBin.path)
let augmentedPath = CodeburnCLI.augmentedPath(
"/usr/bin:/bin",
homeDirectory: home.path,
environment: [:]
)
// Keep the behavior fixture independent of tools installed on the CI host.
// Every retained entry came from the production augmentation above.
let isolatedPath = augmentedPath
.split(separator: ":")
.map(String.init)
.filter { $0 == "/usr/bin" || $0 == "/bin" || $0.hasPrefix(home.path + "/") }
.joined(separator: ":")
let process = Process()
let stdout = Pipe()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = ["--", wrapper.path, "--version"]
process.environment = [
"HOME": home.path,
"PATH": isolatedPath,
]
process.standardOutput = stdout
process.standardError = Pipe()
try process.run()
process.waitUntilExit()
let output = String(decoding: stdout.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
#expect(process.terminationStatus == 0)
#expect(output == "nix-node-ok\n")
}
@Test("nix-darwin per-user profile is derived from the home directory")
func nixDarwinPerUserProfileIsIncluded() {
let path = CodeburnCLI.augmentedPath(
"/usr/bin:/bin",
homeDirectory: "/Users/test",
environment: [:]
)
let entries = path.split(separator: ":").map(String.init)
#expect(entries.contains("/etc/profiles/per-user/test/bin"))
#expect(entries.contains("/run/current-system/sw/bin"))
}
/// Regression: the app picked up whichever `node` came first on the inherited
/// PATH, so an nvm default of v20 shadowed the v24 that installed the CLI and
/// every refresh failed with "codeburn requires Node.js >= 22.13.0".
@Test("interpreter beside the CLI wins over an older node on PATH")
func siblingNodeIsPreferredOverInheritedPath() throws {
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("CodeburnCLIPathTests-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: root) }
let newBin = root.appendingPathComponent("node/v24/bin", isDirectory: true)
let oldBin = root.appendingPathComponent("node/v20/bin", isDirectory: true)
try FileManager.default.createDirectory(at: newBin, withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: oldBin, withIntermediateDirectories: true)
for bin in [newBin, oldBin] {
let node = bin.appendingPathComponent("node")
try "#!/bin/sh\n".write(to: node, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: node.path)
}
let path = CodeburnCLI.augmentedPath(
"\(oldBin.path):/usr/bin:/bin",
homeDirectory: root.appendingPathComponent("home").path,
environment: [:],
resolvedCLI: newBin.appendingPathComponent("codeburn").path
)
let entries = path.split(separator: ":").map(String.init)
#expect(entries.first == newBin.path)
#expect(entries.firstIndex(of: newBin.path)! < entries.firstIndex(of: oldBin.path)!)
// The stale entry still has to survive: it is where the rest of that
// toolchain lives, it just no longer decides which node runs.
#expect(entries.contains(oldBin.path))
}
@Test("a CLI directory already on PATH is promoted, not duplicated")
func siblingNodeDirectoryIsNotDuplicated() throws {
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("CodeburnCLIPathTests-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: root) }
let bin = root.appendingPathComponent("bin", isDirectory: true)
try FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true)
let node = bin.appendingPathComponent("node")
try "#!/bin/sh\n".write(to: node, atomically: true, encoding: .utf8)
try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: node.path)
let path = CodeburnCLI.augmentedPath(
"/usr/bin:\(bin.path):/bin",
homeDirectory: root.appendingPathComponent("home").path,
environment: [:],
resolvedCLI: bin.appendingPathComponent("codeburn").path
)
let entries = path.split(separator: ":").map(String.init)
#expect(entries.first == bin.path)
#expect(entries.filter { $0 == bin.path }.count == 1)
}
/// A bare `codeburn` (PATH lookup) or a directory with no interpreter beside it
/// must leave the inherited order untouched -- reordering PATH on a guess would
/// change which tools every other lookup resolves to.
@Test("PATH order is untouched when there is no sibling interpreter")
func inheritedOrderSurvivesWithoutSiblingNode() throws {
let root = FileManager.default.temporaryDirectory
.appendingPathComponent("CodeburnCLIPathTests-\(UUID().uuidString)", isDirectory: true)
defer { try? FileManager.default.removeItem(at: root) }
let bin = root.appendingPathComponent("bin", isDirectory: true)
try FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true)
for cli in [bin.appendingPathComponent("codeburn").path, "codeburn"] {
let path = CodeburnCLI.augmentedPath(
"/usr/bin:/bin",
homeDirectory: root.appendingPathComponent("home").path,
environment: [:],
resolvedCLI: cli
)
let entries = path.split(separator: ":").map(String.init)
#expect(entries.first == "/usr/bin")
#expect(entries.dropFirst().first == "/bin")
#expect(!entries.contains(bin.path))
}
}
/// #1825: a desktop-only install has no codeburn on PATH and may have no usable record, but
/// the CodeBurn.app it does have carries one.
@Test("resolution order: record, package-manager directories, then a desktop app's CLI")
func resolutionOrder() {
let home = "/Users/test"
let recorded = "\(home)/Library/Application Support/codeburn-desktop/codeburn-desktop-cli.sh"
let homebrew = "/opt/homebrew/bin/codeburn"
let volta = "\(home)/.volta/bin/codeburn"
let system = "/Applications/CodeBurn.app/Contents/Resources/cli/codeburn"
let user = "\(home)/Applications/CodeBurn.app/Contents/Resources/cli/codeburn"
func resolve(_ present: Set<String>) -> (path: String?, tried: [String]) {
CodeburnCLI.resolveInstalledCLI(
recorded: recorded,
homeDirectory: home,
environment: [:],
isExecutable: { present.contains($0) }
)
}
#expect(resolve([recorded, volta, system]).path == recorded)
#expect(resolve([volta, system]).path == volta)
#expect(resolve([system, user]).path == system)
#expect(resolve([user]).path == user)
let none = resolve([])
#expect(none.path == nil)
#expect(none.tried.first == recorded)
#expect(Array(none.tried.suffix(2)) == [system, user])
#expect(none.tried.firstIndex(of: homebrew)! < none.tried.firstIndex(of: volta)!)
#expect(Set(none.tried).count == none.tried.count)
}
@Test("a missing CLI names every path tried; any other failure passes through")
func missingCLINamesEveryPath() {
let tried = ["/opt/homebrew/bin/codeburn", "/Applications/CodeBurn.app/Contents/Resources/cli/codeburn"]
let message = CodeburnCLI.explainedFailure(
code: 127,
stderr: "env: codeburn: No such file or directory\n",
tried: { tried }
)
#expect(message.hasPrefix("No CodeBurn CLI found"))
#expect(message.contains("Tried: /opt/homebrew/bin/codeburn, /Applications/CodeBurn.app/Contents/Resources/cli/codeburn."))
#expect(CodeburnCLI.explainedFailure(code: 1, stderr: "boom", tried: { tried }) == "boom")
}
}