|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "open3" |
| 5 | + |
| 6 | +require "cmd/shared_examples/args_parse" |
| 7 | +require "cmd/as-console-user" |
| 8 | + |
| 9 | +RSpec.describe Homebrew::Cmd::AsConsoleUser do |
| 10 | + let(:as_console_user_script) { HOMEBREW_LIBRARY_PATH/"cmd/as-console-user.sh" } |
| 11 | + let(:repository_root) { HOMEBREW_LIBRARY_PATH.parent.parent } |
| 12 | + let(:test_root) { mktmpdir } |
| 13 | + let(:macos_user_script) { repository_root/"Library/Homebrew/utils/macos_user.sh" } |
| 14 | + |
| 15 | + let(:macos_env) do |
| 16 | + { |
| 17 | + "HOMEBREW_BREW_FILE" => "brew", |
| 18 | + "HOMEBREW_LIBRARY" => (repository_root/"Library").to_s, |
| 19 | + "HOMEBREW_MACOS" => "1", |
| 20 | + } |
| 21 | + end |
| 22 | + |
| 23 | + it_behaves_like "parseable arguments" |
| 24 | + |
| 25 | + def run_as_console_user_shell(script, env = {}) |
| 26 | + Bundler.with_unbundled_env do |
| 27 | + Open3.capture3(env, "/bin/bash", "-c", script) |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + it "prints help and fails when no command is provided" do |
| 32 | + stdout, stderr, status = run_as_console_user_shell( |
| 33 | + <<~SH, |
| 34 | + source "#{as_console_user_script}" |
| 35 | + brew() { printf '%s\\n' "$*" >&2; } |
| 36 | + homebrew-as-console-user |
| 37 | + SH |
| 38 | + "HOMEBREW_BREW_FILE" => "brew", |
| 39 | + ) |
| 40 | + |
| 41 | + expect(status.exitstatus).to eq 1 |
| 42 | + expect(stdout).to be_empty |
| 43 | + expect(stderr).to eq("help as-console-user\n") |
| 44 | + end |
| 45 | + |
| 46 | + it "rejects a root console user" do |
| 47 | + stdout, stderr, status = run_as_console_user_shell( |
| 48 | + <<~SH, |
| 49 | + source "#{as_console_user_script}" |
| 50 | + odie() { echo "Error: $*" >&2; exit 1; } |
| 51 | + stat() { printf 'root\\n'; } |
| 52 | + homebrew-as-console-user install wget |
| 53 | + SH |
| 54 | + macos_env, |
| 55 | + ) |
| 56 | + |
| 57 | + expect(status.exitstatus).to eq 1 |
| 58 | + expect(stdout).to be_empty |
| 59 | + expect(stderr).to eq("Error: No supported macOS console user is logged in.\n") |
| 60 | + end |
| 61 | + |
| 62 | + it "rejects a loginwindow console user" do |
| 63 | + stdout, stderr, status = run_as_console_user_shell( |
| 64 | + <<~SH, |
| 65 | + source "#{as_console_user_script}" |
| 66 | + odie() { echo "Error: $*" >&2; exit 1; } |
| 67 | + stat() { printf 'loginwindow\\n'; } |
| 68 | + homebrew-as-console-user install wget |
| 69 | + SH |
| 70 | + macos_env, |
| 71 | + ) |
| 72 | + |
| 73 | + expect(status.exitstatus).to eq 1 |
| 74 | + expect(stdout).to be_empty |
| 75 | + expect(stderr).to eq("Error: No supported macOS console user is logged in.\n") |
| 76 | + end |
| 77 | + |
| 78 | + it "rejects non-macOS systems" do |
| 79 | + stdout, stderr, status = run_as_console_user_shell( |
| 80 | + <<~SH, |
| 81 | + source "#{as_console_user_script}" |
| 82 | + odie() { echo "Error: $*" >&2; exit 1; } |
| 83 | + homebrew-as-console-user install wget |
| 84 | + SH |
| 85 | + "HOMEBREW_BREW_FILE" => "brew", |
| 86 | + ) |
| 87 | + |
| 88 | + expect(status.exitstatus).to eq 1 |
| 89 | + expect(stdout).to be_empty |
| 90 | + expect(stderr).to eq("Error: `brew as-console-user` is only supported on macOS.\n") |
| 91 | + end |
| 92 | + |
| 93 | + it "uses the package user plist before the console user" do |
| 94 | + homebrew_pkg_user_plist = test_root/".homebrew_pkg_user.plist" |
| 95 | + homebrew_pkg_user_plist.write "plist" |
| 96 | + |
| 97 | + stdout, stderr, status = run_as_console_user_shell( |
| 98 | + <<~SH, |
| 99 | + source "#{macos_user_script}" |
| 100 | + defaults() { printf 'munki\\n'; } |
| 101 | + stat() { printf 'root\\n'; } |
| 102 | + homebrew-package-user |
| 103 | + SH |
| 104 | + "HOMEBREW_PKG_USER_PLIST" => homebrew_pkg_user_plist.to_s, |
| 105 | + ) |
| 106 | + |
| 107 | + expect(status.success?).to be true |
| 108 | + expect(stdout).to eq("munki\n") |
| 109 | + expect(stderr).to be_empty |
| 110 | + end |
| 111 | + |
| 112 | + it "falls back to the console user without a package user plist" do |
| 113 | + stdout, stderr, status = run_as_console_user_shell <<~SH |
| 114 | + source "#{macos_user_script}" |
| 115 | + stat() { printf 'mike\\n'; } |
| 116 | + homebrew-package-user |
| 117 | + SH |
| 118 | + |
| 119 | + expect(status.success?).to be true |
| 120 | + expect(stdout).to eq("mike\n") |
| 121 | + expect(stderr).to be_empty |
| 122 | + end |
| 123 | + |
| 124 | + it "rejects package user lookup without a package user or console user" do |
| 125 | + stdout, stderr, status = run_as_console_user_shell <<~SH |
| 126 | + source "#{macos_user_script}" |
| 127 | + stat() { printf 'root\\n'; } |
| 128 | + homebrew-package-user |
| 129 | + SH |
| 130 | + |
| 131 | + expect(status.exitstatus).to eq 1 |
| 132 | + expect(stdout).to be_empty |
| 133 | + expect(stderr).to be_empty |
| 134 | + end |
| 135 | + |
| 136 | + it "dispatches the nested brew command as the console user" do |
| 137 | + args_file = test_root/"sudo-args.txt" |
| 138 | + console_home = test_root/"console-home" |
| 139 | + console_home.mkpath |
| 140 | + |
| 141 | + stdout, stderr, status = run_as_console_user_shell( |
| 142 | + <<~SH, |
| 143 | + source "#{as_console_user_script}" |
| 144 | + odie() { echo "Error: $*" >&2; exit 1; } |
| 145 | + stat() { printf 'mike\\n'; } |
| 146 | + id() { printf 'mike:*:501:20::0:0:Mike:#{console_home}:/bin/zsh\\n'; } |
| 147 | + sudo() { |
| 148 | + printf 'cwd=%s\\n' "$PWD" > "#{args_file}" |
| 149 | + printf '%s\\n' "$@" >> "#{args_file}" |
| 150 | + return 42 |
| 151 | + } |
| 152 | + homebrew-as-console-user upgrade git --minimum-version=2.50.1 |
| 153 | + SH |
| 154 | + macos_env.merge("HOMEBREW_BREW_FILE" => "/opt/homebrew/bin/brew"), |
| 155 | + ) |
| 156 | + |
| 157 | + expect(status.exitstatus).to eq 42 |
| 158 | + expect(stdout).to be_empty |
| 159 | + expect(stderr).to be_empty |
| 160 | + expect(args_file.read).to eq <<~EOS |
| 161 | + cwd=#{console_home} |
| 162 | + -H |
| 163 | + -u |
| 164 | + mike |
| 165 | + /usr/bin/env |
| 166 | + -i |
| 167 | + HOME=#{console_home} |
| 168 | + USER=mike |
| 169 | + LOGNAME=mike |
| 170 | + PWD=#{console_home} |
| 171 | + PATH=/usr/bin:/bin:/usr/sbin:/sbin |
| 172 | + /opt/homebrew/bin/brew |
| 173 | + upgrade |
| 174 | + git |
| 175 | + --minimum-version=2.50.1 |
| 176 | + EOS |
| 177 | + end |
| 178 | +end |
0 commit comments