Merge pull request #2026 from reitermarkus/spec-analytics
Convert `brew analytics` test to spec.
This commit is contained in:
		
						commit
						c4bfdb5763
					
				@ -1,26 +0,0 @@
 | 
			
		||||
require "testing_env"
 | 
			
		||||
 | 
			
		||||
class IntegrationCommandTestAnalytics < IntegrationCommandTestCase
 | 
			
		||||
  def test_analytics
 | 
			
		||||
    HOMEBREW_REPOSITORY.cd do
 | 
			
		||||
      shutup do
 | 
			
		||||
        system "git", "init"
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    assert_match "Analytics is disabled (by HOMEBREW_NO_ANALYTICS)",
 | 
			
		||||
      cmd("analytics", "HOMEBREW_NO_ANALYTICS" => "1")
 | 
			
		||||
 | 
			
		||||
    cmd("analytics", "off")
 | 
			
		||||
    assert_match "Analytics is disabled",
 | 
			
		||||
      cmd("analytics", "HOMEBREW_NO_ANALYTICS" => nil)
 | 
			
		||||
 | 
			
		||||
    cmd("analytics", "on")
 | 
			
		||||
    assert_match "Analytics is enabled", cmd("analytics",
 | 
			
		||||
      "HOMEBREW_NO_ANALYTICS" => nil)
 | 
			
		||||
 | 
			
		||||
    assert_match "Invalid usage", cmd_fail("analytics", "on", "off")
 | 
			
		||||
    assert_match "Invalid usage", cmd_fail("analytics", "testball")
 | 
			
		||||
    cmd("analytics", "regenerate-uuid")
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										52
									
								
								Library/Homebrew/test/cmd/analytics_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								Library/Homebrew/test/cmd/analytics_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
			
		||||
describe "brew analytics", :integration_test do
 | 
			
		||||
  before(:each) do
 | 
			
		||||
    HOMEBREW_REPOSITORY.cd do
 | 
			
		||||
      shutup do
 | 
			
		||||
        system "git", "init"
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "is disabled when HOMEBREW_NO_ANALYTICS is set" do
 | 
			
		||||
    expect { brew "analytics", "HOMEBREW_NO_ANALYTICS" => "1" }
 | 
			
		||||
      .to output(/Analytics is disabled \(by HOMEBREW_NO_ANALYTICS\)/).to_stdout
 | 
			
		||||
      .and not_to_output.to_stderr
 | 
			
		||||
      .and be_a_success
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  context "when HOMEBREW_NO_ANALYTICS is unset" do
 | 
			
		||||
    it "is disabled after running `brew analytics off`" do
 | 
			
		||||
      brew "analytics", "off"
 | 
			
		||||
      expect { brew "analytics", "HOMEBREW_NO_ANALYTICS" => nil }
 | 
			
		||||
        .to output(/Analytics is disabled/).to_stdout
 | 
			
		||||
        .and not_to_output.to_stderr
 | 
			
		||||
        .and be_a_success
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "is enabled after running `brew analytics on`" do
 | 
			
		||||
      brew "analytics", "on"
 | 
			
		||||
      expect { brew "analytics", "HOMEBREW_NO_ANALYTICS" => nil }
 | 
			
		||||
        .to output(/Analytics is enabled/).to_stdout
 | 
			
		||||
        .and not_to_output.to_stderr
 | 
			
		||||
        .and be_a_success
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "fails when running `brew analytics on off`" do
 | 
			
		||||
    expect { brew "analytics", "on", "off" }
 | 
			
		||||
      .to output(/Invalid usage/).to_stderr
 | 
			
		||||
      .and not_to_output.to_stdout
 | 
			
		||||
      .and be_a_failure
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "fails when running `brew analytics testball`" do
 | 
			
		||||
    expect { brew "analytics", "testball" }
 | 
			
		||||
      .to output(/Invalid usage/).to_stderr
 | 
			
		||||
      .and not_to_output.to_stdout
 | 
			
		||||
      .and be_a_failure
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  it "can generate a new UUID" do
 | 
			
		||||
    expect { brew "analytics", "regenerate-uuid" }.to be_a_success
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -16,6 +16,7 @@ require "tap"
 | 
			
		||||
 | 
			
		||||
require "test/support/helper/shutup"
 | 
			
		||||
require "test/support/helper/fixtures"
 | 
			
		||||
require "test/support/helper/spec/shared_context/integration_test"
 | 
			
		||||
 | 
			
		||||
TEST_DIRECTORIES = [
 | 
			
		||||
  CoreTap.instance.path/"Formula",
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,94 @@
 | 
			
		||||
require "rspec"
 | 
			
		||||
require "open3"
 | 
			
		||||
 | 
			
		||||
RSpec::Matchers.define_negated_matcher :not_to_output, :output
 | 
			
		||||
RSpec::Matchers.define_negated_matcher :be_a_failure, :be_a_success
 | 
			
		||||
 | 
			
		||||
RSpec.shared_context "integration test" do
 | 
			
		||||
  extend RSpec::Matchers::DSL
 | 
			
		||||
 | 
			
		||||
  matcher :be_a_success do
 | 
			
		||||
    match do |actual|
 | 
			
		||||
      status = actual.is_a?(Proc) ? actual.call : actual
 | 
			
		||||
      status.respond_to?(:success?) && status.success?
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def supports_block_expectations?
 | 
			
		||||
      true
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # It needs to be nested like this:
 | 
			
		||||
    #
 | 
			
		||||
    #   expect {
 | 
			
		||||
    #     expect {
 | 
			
		||||
    #       # command
 | 
			
		||||
    #     }.to be_a_success
 | 
			
		||||
    #   }.to output(something).to_stdout
 | 
			
		||||
    #
 | 
			
		||||
    # rather than this:
 | 
			
		||||
    #
 | 
			
		||||
    #   expect {
 | 
			
		||||
    #     expect {
 | 
			
		||||
    #       # command
 | 
			
		||||
    #     }.to output(something).to_stdout
 | 
			
		||||
    #   }.to be_a_success
 | 
			
		||||
    #
 | 
			
		||||
    def expects_call_stack_jump?
 | 
			
		||||
      true
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  before(:each) do
 | 
			
		||||
    (HOMEBREW_PREFIX/"bin").mkpath
 | 
			
		||||
    FileUtils.touch HOMEBREW_PREFIX/"bin/brew"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  after(:each) do
 | 
			
		||||
    FileUtils.rm HOMEBREW_PREFIX/"bin/brew"
 | 
			
		||||
    FileUtils.rmdir HOMEBREW_PREFIX/"bin"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Generate unique ID to be able to
 | 
			
		||||
  # properly merge coverage results.
 | 
			
		||||
  def command_id_from_args(args)
 | 
			
		||||
    @command_count ||= 0
 | 
			
		||||
    pretty_args = args.join(" ").gsub(TEST_TMPDIR, "@TMPDIR@")
 | 
			
		||||
    file_and_line = caller[1].sub(/(.*\d+):.*/, '\1')
 | 
			
		||||
                             .sub("#{HOMEBREW_LIBRARY_PATH}/test/", "")
 | 
			
		||||
    "#{file_and_line}:brew #{pretty_args}:#{@command_count += 1}"
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Runs a `brew` command with the test configuration
 | 
			
		||||
  # and with coverage reporting enabled.
 | 
			
		||||
  def brew(*args)
 | 
			
		||||
    env = args.last.is_a?(Hash) ? args.pop : {}
 | 
			
		||||
 | 
			
		||||
    env.merge!(
 | 
			
		||||
      "HOMEBREW_BREW_FILE" => HOMEBREW_PREFIX/"bin/brew",
 | 
			
		||||
      "HOMEBREW_INTEGRATION_TEST" => command_id_from_args(args),
 | 
			
		||||
      "HOMEBREW_TEST_TMPDIR" => TEST_TMPDIR,
 | 
			
		||||
      "HOMEBREW_DEVELOPER" => ENV["HOMEBREW_DEVELOPER"],
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    ruby_args = [
 | 
			
		||||
      "-W0",
 | 
			
		||||
      "-I", "#{HOMEBREW_LIBRARY_PATH}/test/support/lib",
 | 
			
		||||
      "-I", HOMEBREW_LIBRARY_PATH.to_s,
 | 
			
		||||
      "-rconfig"
 | 
			
		||||
    ]
 | 
			
		||||
    ruby_args << "-rsimplecov" if ENV["HOMEBREW_TESTS_COVERAGE"]
 | 
			
		||||
    ruby_args << "-rtest/support/helper/integration_mocks"
 | 
			
		||||
    ruby_args << (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path.to_s
 | 
			
		||||
 | 
			
		||||
    Bundler.with_original_env do
 | 
			
		||||
      stdout, stderr, status = Open3.capture3(env, RUBY_PATH, *ruby_args, *args)
 | 
			
		||||
      $stdout.print stdout
 | 
			
		||||
      $stderr.print stderr
 | 
			
		||||
      status
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
RSpec.configure do |config|
 | 
			
		||||
  config.include_context "integration test", :integration_test
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user