| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe Cask::Utils do | 
					
						
							| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  |   let(:command) { NeverSudoSystemCommand } | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  |   let(:dir) { mktmpdir } | 
					
						
							|  |  |  |   let(:path) { dir/"a/b/c" } | 
					
						
							|  |  |  |   let(:link) { dir/"link" } | 
					
						
							| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "::gain_permissions_mkpath" do | 
					
						
							|  |  |  |     it "creates a directory" do | 
					
						
							|  |  |  |       expect(path).not_to exist | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       described_class.gain_permissions_mkpath(path, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  |       expect(path).to be_a_directory | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       described_class.gain_permissions_mkpath(path, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  |       expect(path).to be_a_directory | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when parent directory is not writable" do | 
					
						
							|  |  |  |       it "creates a directory with `sudo`" do | 
					
						
							|  |  |  |         FileUtils.chmod "-w", dir | 
					
						
							|  |  |  |         expect(dir).not_to be_writable | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(command).to receive(:run!).exactly(:once).and_wrap_original do |original, *args, **options| | 
					
						
							|  |  |  |           FileUtils.chmod "+w", dir | 
					
						
							|  |  |  |           original.call(*args, **options) | 
					
						
							|  |  |  |           FileUtils.chmod "-w", dir | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(path).not_to exist | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         described_class.gain_permissions_mkpath(path, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  |         expect(path).to be_a_directory | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |         described_class.gain_permissions_mkpath(path, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  |         expect(path).to be_a_directory | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(dir).not_to be_writable | 
					
						
							|  |  |  |         FileUtils.chmod "+w", dir | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "::gain_permissions_remove" do | 
					
						
							|  |  |  |     it "removes the symlink, not the file it points to" do | 
					
						
							|  |  |  |       path.dirname.mkpath | 
					
						
							|  |  |  |       FileUtils.touch path | 
					
						
							|  |  |  |       FileUtils.ln_s path, link | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(path).to be_a_file | 
					
						
							|  |  |  |       expect(link).to be_a_symlink | 
					
						
							|  |  |  |       expect(link.realpath).to eq path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       described_class.gain_permissions_remove(link, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(path).to be_a_file | 
					
						
							|  |  |  |       expect(link).not_to exist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       described_class.gain_permissions_remove(path, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(path).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it "removes the symlink, not the directory it points to" do | 
					
						
							|  |  |  |       path.mkpath | 
					
						
							|  |  |  |       FileUtils.ln_s path, link | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(path).to be_a_directory | 
					
						
							|  |  |  |       expect(link).to be_a_symlink | 
					
						
							|  |  |  |       expect(link.realpath).to eq path | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       described_class.gain_permissions_remove(link, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(path).to be_a_directory | 
					
						
							|  |  |  |       expect(link).not_to exist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       described_class.gain_permissions_remove(path, command:) | 
					
						
							| 
									
										
										
										
											2023-05-13 03:41:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(path).not_to exist | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2023-05-13 03:11:51 +02:00
										 |  |  | end |