Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
import Foundation
@testable import PeelCore
import Testing
struct ToolSettingsTests {
@Test func readsAnNpmrcAsNpmDoes() {
let npmrc = """
; written by npm config set
# and edited by hand
cache=/first
cache = "/Volumes/Fast/npm cache"
store-dir = /Volumes/Fast/pnpm ; moved to the fast disk
escaped = /a\\#b
quoted = '/single'
[section]
cache = /inside/a/section
"""
#expect(ToolSettings.iniValue(of: "cache", in: npmrc) == "/Volumes/Fast/npm cache")
#expect(ToolSettings.iniValue(of: "store-dir", in: npmrc) == "/Volumes/Fast/pnpm")
#expect(ToolSettings.iniValue(of: "escaped", in: npmrc) == "/a#b")
#expect(ToolSettings.iniValue(of: "quoted", in: npmrc) == "/single")
#expect(ToolSettings.iniValue(of: "registry", in: npmrc) == nil)
}
@Test func readsAYarnrcAsYarnOneDoes() {
let yarnrc = """
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
cache-folder "/Volumes/Fast/yarn cache"
lastUpdateCheck 1791158856071
--cache-folder /Volumes/Faster/yarn
indented "/is/inside/a/block"
"""
#expect(ToolSettings.yarnrcValue(of: "cache-folder", in: yarnrc) == "/Volumes/Fast/yarn cache")
#expect(ToolSettings.yarnrcValue(of: "--cache-folder", in: yarnrc) == "/Volumes/Faster/yarn")
#expect(ToolSettings.yarnrcValue(of: "lastUpdateCheck", in: yarnrc) == nil)
#expect(ToolSettings.yarnrcValue(of: "indented", in: yarnrc) == nil)
}
@Test func readsATopLevelYamlScalar() {
let yaml = """
# global settings
storeDir: /Volumes/Fast/pnpm # moved
globalFolder: "/Volumes/Fast/yarn berry"
cacheFolder: '/it''s here'
packageExtensions:
storeDir: /nested/is/no/setting/of/the/top
npmScopes: { example: { npmRegistryServer: "https://example.com" } }
"""
#expect(ToolSettings.yamlValue(of: "storeDir", in: yaml) == "/Volumes/Fast/pnpm")
#expect(ToolSettings.yamlValue(of: "globalFolder", in: yaml) == "/Volumes/Fast/yarn berry")
#expect(ToolSettings.yamlValue(of: "cacheFolder", in: yaml) == "/it's here")
#expect(ToolSettings.yamlValue(of: "packageExtensions", in: yaml) == nil)
#expect(ToolSettings.yamlValue(of: "npmScopes", in: yaml) == nil)
}
@Test func readsTheFileGoEnvWritesAsGoDoes() {
let environment = "GOMODCACHE=/first\ngoproxy=a name in lowercase is no setting\nGOMODCACHE=/Volumes/Fast/go mod\n"
#expect(ToolSettings.goEnvironmentValue(of: "GOMODCACHE", in: environment) == "/Volumes/Fast/go mod")
#expect(ToolSettings.goEnvironmentValue(of: "goproxy", in: environment) == nil)
}
/// npm and pnpm 10 read `${HOME}` and `~/`, Yarn its file's folder and a fallback, pnpm 11 `~/`, Go a full path.
@Test func readsARemotesAddressAsGitDoes() {
let config = """
[core]
\turl = https://example.org/not-a-remote.git
[Remote "origin"]
\tfetch = +refs/heads/*:refs/remotes/origin/*
\tURL = "https://github.com/CocoaPods/Specs.git" ; the public index
[remote "Origin"]
\turl = https://example.org/another.git
"""
#expect(
ToolSettings.gitValue(of: "url", inSection: "remote", named: "origin", in: config)
== "https://github.com/CocoaPods/Specs.git"
)
#expect(ToolSettings.gitValue(of: "url", inSection: "remote", named: "upstream", in: config) == nil)
#expect(ToolSettings.gitValue(of: "url", inSection: "core", in: config) == "https://example.org/not-a-remote.git")
for address in [
"https://github.com/CocoaPods/Specs.git", "https://github.com/cocoapods/specs/", "[email protected]:CocoaPods/Specs.git",
"ssh://[email protected]/CocoaPods/Specs",
] {
#expect(ToolSettings.gitAddress(address) == "github.com/cocoapods/specs", "\(address)")
}
#expect(ToolSettings.gitAddress("https://github.com/CocoaPods/Specs-mirror.git") != "github.com/cocoapods/specs")
}
@Test func takesEachToolsPathAsTheToolDoes() throws {
let directory = try TemporaryDirectory()
try directory.file("home/.npmrc", contents: Data("cache=${HOME}/npm-cache\nstore-dir=~/pnpm-ten\n".utf8))
try directory.file("home/.yarnrc", contents: Data("cache-folder relative/yarn\n".utf8))
try directory.file("home/.yarnrc.yml", contents: Data("globalFolder: ${HOME:-/nowhere}/berry\n".utf8))
try directory.file("home/Library/Preferences/pnpm/config.yaml", contents: Data("storeDir: ~/pnpm-eleven\n".utf8))
try directory.file("home/Library/Preferences/pnpm/rc", contents: Data("store-dir=/Volumes/Fast/pnpm\n".utf8))
let go = "GOPATH=/Volumes/Fast/go:/Volumes/Other/go\nGOMODCACHE=\nGOCACHE=off\n"
try directory.file("home/Library/Application Support/go/env", contents: Data(go.utf8))
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
let inHome = { (path: String) in home.appending(path: path).path(percentEncoded: false) }
let settings = ToolSettings(home: home)
#expect(Self.path(settings.npmCache) == inHome("npm-cache"))
#expect(Self.path(settings.yarnCacheRoot) == inHome("relative/yarn"))
#expect(Self.path(settings.yarnGlobalFolder) == inHome("berry"))
#expect(settings.pnpmStoreFolders.map { Self.path($0) } == [
inHome("pnpm-eleven"), "/Volumes/Fast/pnpm", inHome("pnpm-ten"),
])
#expect(Self.path(settings.goModuleCache) == "/Volumes/Fast/go/pkg/mod")
#expect(settings.goBuildCache == nil)
}
/// Yarn 1 takes `--cache-folder` in `.yarnrc` as typed on the command line, then its own setting, then npm's.
@Test func findsYarnOnesCacheWhereYarnOneLooksFirst() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
try directory.file("home/.npmrc", contents: Data("cache-folder=/from/npm\n".utf8))
#expect(Self.path(ToolSettings(home: home).yarnCacheRoot) == "/from/npm")
try directory.file("home/.yarnrc", contents: Data("--cache-folder relative/argument\ncache-folder /from/yarn\n".utf8))
#expect(Self.path(ToolSettings(home: home).yarnCacheRoot) == "/from/yarn", "a relative argument is no place")
try directory.file("home/.yarnrc", contents: Data("--cache-folder /from/argument\ncache-folder /from/yarn\n".utf8))
#expect(Self.path(ToolSettings(home: home).yarnCacheRoot) == "/from/argument")
}
@Test func passesOverWhatOnlyTheToolsEnvironmentCouldName() throws {
let directory = try TemporaryDirectory()
try directory.file("home/.npmrc", contents: Data("cache=${XDG_CACHE_HOME}/npm\nstore-dir=relative/pnpm\n".utf8))
try directory.file("home/.yarnrc.yml", contents: Data("globalFolder: ${XDG_DATA_HOME:-${HOME}}/berry\n".utf8))
try directory.file("home/Library/Preferences/pnpm/config.yaml", contents: Data("storeDir: ${HOME}/pnpm\n".utf8))
try directory.file("home/Library/Application Support/go/env", contents: Data("GOMODCACHE=go/mod\n".utf8))
let settings = ToolSettings(home: directory.url.appending(path: "home", directoryHint: .isDirectory))
#expect(settings.npmCache == nil)
#expect(settings.yarnGlobalFolder == nil)
#expect(settings.pnpmStoreFolders.isEmpty)
#expect(settings.goModuleCache == nil)
}
/// pnpm takes a store folder that already ends with its version as the store itself.
@Test func aStoreFolderNamedForItsVersionIsTheStore() async throws {
let directory = try TemporaryDirectory()
let store = directory.url.appending(path: "Fast/pnpm/v11").path(percentEncoded: false)
try directory.file("home/Library/Preferences/pnpm/config.yaml", contents: Data("storeDir: \(store)\n".utf8))
try directory.file("Fast/pnpm/v11/index.db")
let pnpm = DeveloperCaches.definitions.filter { $0.id == "pnpm" }
let found = await DeveloperCaches.scan(pnpm, homeDirectory: directory.url.appending(path: "home"))
#expect(found.flatMap(\.locations).map(\.url.path) == [store])
}
/// Space leaves to Developer the folder a tool's configuration moved a cache into, as it leaves the usual one.
@Test func spaceLeavesToDeveloperACacheMovedIntoItsCaches() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
let moved = home.appending(path: "Library/Caches/npm").path(percentEncoded: false)
try directory.file("home/.npmrc", contents: Data("cache = \(moved)\n".utf8))
try directory.file("home/Library/Caches/npm/_cacache/index-v5/00/entry")
let left = DeveloperCaches.FoldersLeftToDeveloper(home: home)
#expect(left.inside(home.appending(path: "Library/Caches", directoryHint: .isDirectory)).contains(["npm"]))
}
@Test func findsHomebrewsCacheWhereItsSettingsFilesMoveIt() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
let write = { (path: String, text: String) throws in _ = try directory.file(path, contents: Data(text.utf8)) }
try write("etc/homebrew/brew.env", "HOMEBREW_CACHE=/for/everyone\n")
try write("usr/local/etc/homebrew/brew.env", "HOMEBREW_CACHE=/for/intel\n")
try write("home/.homebrew/brew.env", "HOMEBREW_CACHE=/own\n HOMEBREW_CACHE=/own/last\n")
try write("home/.config/homebrew/brew.env", "HOMEBREW_CACHE=/unfinished")
let caches = { ToolSettings(home: home, root: directory.url).homebrewCaches.map { Self.path($0) } }
#expect(caches() == ["/own/last", "/for/everyone", "/for/intel"])
try write("etc/homebrew/brew.env", "HOMEBREW_CACHE=/for/everyone\nHOMEBREW_SYSTEM_ENV_TAKES_PRIORITY=1\n")
#expect(caches() == ["/for/everyone"], "the system's file is read again last")
try write("etc/homebrew/brew.env", "")
try write("usr/local/etc/homebrew/brew.env", "HOMEBREW_CACHE=\n")
try write("home/.homebrew/brew.env", "HOMEBREW_CACHE=\"/quoted\"\n")
try write("home/.config/homebrew/brew.env", "HOMEBREW_CACHE=~/Caches\nHOMEBREW_CACHE=$HOME/Caches\n")
#expect(caches().isEmpty, "a value taken as written names no place")
}
@Test func findsHomebrewsCacheInTheSettingsFolderItsOwnFilesName() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
let settings = directory.url.appending(path: "settings", directoryHint: .isDirectory).path(percentEncoded: false)
try directory.file("opt/homebrew/etc/homebrew/brew.env", contents: Data("HOMEBREW_XDG_CONFIG_HOME=\(settings)\n".utf8))
try directory.file("settings/homebrew/brew.env", contents: Data("HOMEBREW_CACHE=/moved\n".utf8))
#expect(ToolSettings(home: home, root: directory.url).homebrewCaches.map { Self.path($0) } == ["/moved"])
}
@Test func spaceLeavesToDeveloperHomebrewsCacheMovedIntoItsCaches() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
let moved = home.appending(path: "Library/Caches/brew").path(percentEncoded: false)
try directory.file("etc/homebrew/brew.env", contents: Data("HOMEBREW_CACHE=\(moved)\n".utf8))
try directory.directory("home/Library/Caches/brew/api")
try directory.file("home/Library/Caches/brew/downloads/0a1b--tool.tar.gz")
let left = DeveloperCaches.FoldersLeftToDeveloper(home: home, root: directory.url)
#expect(left.inside(home.appending(path: "Library/Caches", directoryHint: .isDirectory)).contains(["brew"]))
}
@Test func readsJansDataFolderWhereJanLooksForIt() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
let write = { (path: String, text: String) throws in _ = try directory.file(path, contents: Data(text.utf8)) }
let dataFolder = { Self.path(ToolSettings(home: home).janDataFolder) }
try write("home/Library/Application Support/jan.ai.app/settings.json", #"{"data_folder":"/Volumes/Fast/jan-old"}"#)
#expect(dataFolder() == "/Volumes/Fast/jan-old", "Jan recovers its settings from its bundle's folder")
try write("home/Library/Application Support/Jan/settings.json", #"{"data_folder":"/Volumes/Fast/jan"}"#)
#expect(dataFolder() == "/Volumes/Fast/jan")
try write("home/Library/Application Support/Jan/settings.json", #"{"data_folder":"./data"}"#)
#expect(dataFolder() == nil, "a relative path names no place")
try write("home/Library/Application Support/Jan/settings.json", #"["/Volumes/Fast/jan"]"#)
#expect(dataFolder() == nil)
}
private static func path(_ url: URL?) -> String? {
url.map { $0.path(percentEncoded: false) }.map { $0.count > 1 && $0.hasSuffix("/") ? String($0.dropLast()) : $0 }
}
}