|
35 | 35 | it "generates completion scripts for default shells" do |
36 | 36 | artifact = cask.artifacts.grep(klass).first |
37 | 37 |
|
38 | | - allow(Utils).to receive(:safe_popen_read) do |env, *_args, **_opts| |
39 | | - "#{env.fetch("SHELL")} completion output" |
| 38 | + allow(Sandbox).to receive_messages(ensure_sandbox_installed!: nil, available?: true) |
| 39 | + allow(Sandbox).to receive(:new) do |
| 40 | + instance_double(Sandbox).tap do |sandbox| |
| 41 | + allow(sandbox).to receive(:allow_read) |
| 42 | + allow(sandbox).to receive(:allow_write_temp_and_cache) |
| 43 | + allow(sandbox).to receive(:deny_all_network) |
| 44 | + allow(sandbox).to receive(:run) do |*args| |
| 45 | + Pathname(args.fetch(6)).write("#{args.fetch(1).delete_prefix("SHELL=")} completion output") |
| 46 | + end |
| 47 | + end |
40 | 48 | end |
41 | 49 |
|
42 | 50 | artifact.install_phase |
|
49 | 57 | expect((fish_dir/"foo.fish").read).to eq("fish completion output") |
50 | 58 | end |
51 | 59 |
|
| 60 | + it "sandboxes completion generation" do |
| 61 | + artifact = cask.artifacts.grep(klass).first |
| 62 | + sandboxes = [] |
| 63 | + |
| 64 | + allow(Sandbox).to receive_messages(ensure_sandbox_installed!: nil, available?: true) |
| 65 | + allow(Sandbox).to receive(:new) do |
| 66 | + instance_double(Sandbox).tap do |sandbox| |
| 67 | + expect(sandbox).to receive(:allow_read).with(path: staged_path, type: :subpath) |
| 68 | + expect(sandbox).to receive(:allow_write_temp_and_cache) |
| 69 | + expect(sandbox).to receive(:deny_all_network) |
| 70 | + allow(sandbox).to receive(:run) { |*args| Pathname(args.fetch(6)).write("completion") } |
| 71 | + sandboxes << sandbox |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + artifact.install_phase |
| 76 | + |
| 77 | + expect(sandboxes.length).to eq(3) |
| 78 | + end |
| 79 | + |
| 80 | + it "does not sandbox when HOMEBREW_NO_SANDBOX_CASK is set" do |
| 81 | + artifact = cask.artifacts.grep(klass).first |
| 82 | + |
| 83 | + ENV["HOMEBREW_NO_SANDBOX_CASK"] = "1" |
| 84 | + allow(Sandbox).to receive(:available?).and_return(true) |
| 85 | + allow(Utils).to receive(:safe_popen_read) { |env, *_args, **_opts| "#{env.fetch("SHELL")} completion" } |
| 86 | + expect(Sandbox).not_to receive(:new) |
| 87 | + |
| 88 | + artifact.install_phase |
| 89 | + |
| 90 | + expect((bash_dir/"foo").read).to eq("bash completion") |
| 91 | + ensure |
| 92 | + ENV["HOMEBREW_NO_SANDBOX_CASK"] = nil |
| 93 | + end |
| 94 | + |
52 | 95 | context "when generation fails for one shell" do |
53 | 96 | it "warns and continues generating other shells" do |
54 | 97 | artifact = cask.artifacts.grep(klass).first |
55 | 98 |
|
56 | | - allow(Utils).to receive(:safe_popen_read) do |env, *_args, **_opts| |
57 | | - raise "boom" if env.fetch("SHELL") == "bash" |
58 | | - |
59 | | - "zsh completion" |
| 99 | + allow(Sandbox).to receive_messages(ensure_sandbox_installed!: nil, available?: true) |
| 100 | + allow(Sandbox).to receive(:new) do |
| 101 | + instance_double(Sandbox).tap do |sandbox| |
| 102 | + allow(sandbox).to receive(:allow_read) |
| 103 | + allow(sandbox).to receive(:allow_write_temp_and_cache) |
| 104 | + allow(sandbox).to receive(:deny_all_network) |
| 105 | + allow(sandbox).to receive(:run) do |*args| |
| 106 | + raise "boom" if args.fetch(1) == "SHELL=bash" |
| 107 | + |
| 108 | + Pathname(args.fetch(6)).write("zsh completion") |
| 109 | + end |
| 110 | + end |
60 | 111 | end |
61 | 112 |
|
62 | 113 | expect { artifact.install_phase } |
|
103 | 154 | artifact = cask.artifacts.grep(klass).first |
104 | 155 | captured_args = nil |
105 | 156 |
|
106 | | - allow(Utils).to receive(:safe_popen_read) do |_env, *args, **_opts| |
107 | | - captured_args = args |
108 | | - "zsh completion" |
| 157 | + allow(Sandbox).to receive_messages(ensure_sandbox_installed!: nil, available?: true) |
| 158 | + allow(Sandbox).to receive(:new) do |
| 159 | + instance_double(Sandbox).tap do |sandbox| |
| 160 | + allow(sandbox).to receive(:allow_read) |
| 161 | + allow(sandbox).to receive(:allow_write_temp_and_cache) |
| 162 | + allow(sandbox).to receive(:deny_all_network) |
| 163 | + allow(sandbox).to receive(:run) do |*args| |
| 164 | + captured_args = args |
| 165 | + Pathname(args.fetch(6)).write("zsh completion") |
| 166 | + end |
| 167 | + end |
109 | 168 | end |
110 | 169 |
|
111 | 170 | artifact.install_phase |
112 | 171 |
|
113 | 172 | expect(captured_args).to include("--shell=zsh") |
| 173 | + expect(captured_args.fetch(4)).to end_with(" 2>/dev/null") |
114 | 174 | expect(zsh_dir/"_bar").to be_a_file |
115 | 175 | expect(bash_dir/"bar").not_to exist |
116 | 176 | expect(fish_dir/"bar.fish").not_to exist |
|
0 commit comments