| 
									
										
										
										
											2017-02-03 14:05:07 +01:00
										 |  |  | require "open3" | 
					
						
							|  |  |  | require "rubygems" | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-03 10:49:58 +02:00
										 |  |  | require_relative "shared_examples/invalid_option" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-06 04:31:16 +01:00
										 |  |  | describe Hbc::CLI::Style, :cask do | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   let(:args) { [] } | 
					
						
							| 
									
										
										
										
											2017-05-20 03:46:52 +02:00
										 |  |  |   let(:cli) { described_class.new(*args) } | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-03 10:49:58 +02:00
										 |  |  |   it_behaves_like "a command that handles invalid options" | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe "#run" do | 
					
						
							|  |  |  |     subject { cli.run } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       allow(cli).to receive_messages(install_rubocop: nil, | 
					
						
							|  |  |  |                                      system:          nil, | 
					
						
							|  |  |  |                                      rubocop_args:    nil, | 
					
						
							|  |  |  |                                      cask_paths:      nil) | 
					
						
							|  |  |  |       allow($CHILD_STATUS).to receive(:success?).and_return(success) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when rubocop succeeds" do | 
					
						
							|  |  |  |       let(:success) { true } | 
					
						
							| 
									
										
										
										
											2018-01-27 23:29:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it "does not raise an error" do | 
					
						
							|  |  |  |         expect { subject }.not_to raise_error | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when rubocop fails" do | 
					
						
							|  |  |  |       let(:success) { false } | 
					
						
							| 
									
										
										
										
											2017-05-20 03:46:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it "raises an error" do | 
					
						
							|  |  |  |         expect { subject }.to raise_error(Hbc::CaskError) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#install_rubocop" do | 
					
						
							|  |  |  |     subject { cli.install_rubocop } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when installation succeeds" do | 
					
						
							|  |  |  |       before do | 
					
						
							| 
									
										
										
										
											2017-05-07 17:28:39 +01:00
										 |  |  |         allow(Homebrew).to receive(:install_gem_setup_path!) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "exits successfully" do | 
					
						
							| 
									
										
										
										
											2016-08-27 11:52:14 +02:00
										 |  |  |         expect { subject }.not_to raise_error | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when installation fails" do | 
					
						
							|  |  |  |       before do | 
					
						
							| 
									
										
										
										
											2017-05-07 17:28:39 +01:00
										 |  |  |         allow(Homebrew).to receive(:install_gem_setup_path!).and_raise(SystemExit) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "raises an error" do | 
					
						
							|  |  |  |         expect { subject }.to raise_error(Hbc::CaskError) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2017-02-03 14:05:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 20:51:51 +02:00
										 |  |  |     specify "`rubocop-cask` supports `HOMEBREW_RUBOCOP_VERSION`", :needs_network do | 
					
						
							|  |  |  |       stdout, status = Open3.capture2("gem", "dependency", "rubocop-cask", "--version", HOMEBREW_RUBOCOP_CASK_VERSION, "--pipe", "--remote") | 
					
						
							| 
									
										
										
										
											2017-02-03 14:05:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 20:51:51 +02:00
										 |  |  |       expect(status).to be_a_success | 
					
						
							| 
									
										
										
										
											2017-02-03 14:05:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 20:51:51 +02:00
										 |  |  |       requirement = Gem::Requirement.new(stdout.scan(/rubocop --version '(.*)'/).flatten.first) | 
					
						
							|  |  |  |       version = Gem::Version.new(HOMEBREW_RUBOCOP_VERSION) | 
					
						
							| 
									
										
										
										
											2017-02-03 14:05:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-05 20:51:51 +02:00
										 |  |  |       expect(requirement).not_to be_none | 
					
						
							|  |  |  |       expect(requirement).to be_satisfied_by(version) | 
					
						
							| 
									
										
										
										
											2017-02-03 14:05:07 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#cask_paths" do | 
					
						
							|  |  |  |     subject { cli.cask_paths } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							| 
									
										
										
										
											2017-05-21 02:32:46 +02:00
										 |  |  |       allow(cli).to receive(:args).and_return(tokens) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when no cask tokens are given" do | 
					
						
							|  |  |  |       let(:tokens) { [] } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       before do | 
					
						
							|  |  |  |         allow(Hbc).to receive(:all_tapped_cask_dirs).and_return(%w[Casks MoreCasks]) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it { is_expected.to eq(%w[Casks MoreCasks]) } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when at least one cask token is a path that exists" do | 
					
						
							|  |  |  |       let(:tokens) { ["adium", "Casks/dropbox.rb"] } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       before do | 
					
						
							|  |  |  |         allow(File).to receive(:exist?).and_return(false, true) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "treats all tokens as paths" do | 
					
						
							|  |  |  |         expect(subject).to eq(tokens) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when no cask tokens are paths that exist" do | 
					
						
							|  |  |  |       let(:tokens) { %w[adium dropbox] } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       before do | 
					
						
							|  |  |  |         allow(File).to receive(:exist?).and_return(false) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "tries to find paths for all tokens" do | 
					
						
							| 
									
										
										
										
											2017-06-13 17:14:01 +02:00
										 |  |  |         expect(Hbc::CaskLoader).to receive(:load).twice.and_return(double("cask", sourcefile_path: nil)) | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |         subject | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#rubocop_args" do | 
					
						
							|  |  |  |     subject { cli.rubocop_args } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       allow(cli).to receive(:fix?).and_return(fix) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when fix? is true" do | 
					
						
							|  |  |  |       let(:fix) { true } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       it { is_expected.to include("--auto-correct") } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context "when fix? is false" do | 
					
						
							|  |  |  |       let(:fix) { false } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       it { is_expected.not_to include("--auto-correct") } | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#default_args" do | 
					
						
							|  |  |  |     subject { cli.default_args } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 23:29:55 +01:00
										 |  |  |     it { is_expected.to include("--require", "rubocop-cask", "--format", "simple") } | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "#autocorrect_args" do | 
					
						
							|  |  |  |     subject { cli.autocorrect_args } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     let(:default_args) { ["--format", "simple"] } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  |     it "adds --auto-correct to default args" do | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       allow(cli).to receive(:default_args).and_return(default_args) | 
					
						
							|  |  |  |       expect(subject).to include("--auto-correct", *default_args) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |