class Avm::Git::RevisionTest

def banner

def banner
  infov 'Revision to test', sha1
  ::EacRubyUtils::Speaker.context.on(::EacCli::Speaker.new(err_line_prefix: '  ')) do
    revision_banner
  end
end

def checkout_revision

def checkout_revision
  infom 'Checking out revision...'
  git.execute!('checkout', sha1)
end

def commit_uncached

def commit_uncached
  ::Avm::Git::Commit.new(git, sha1)
end

def git_absolute_path

def git_absolute_path
  ::File.expand_path(git.to_s)
end

def revision_banner

def revision_banner
  infov '* Subject', commit.subject
  infov '* Success?', successful_label
  infov '* STDOUT', stdout_cache.content_path
  infov '* STDERR', stderr_cache.content_path
end

def root_cache

def root_cache
  fs_cache.child(git_absolute_path.parameterize, sha1,
                 options.fetch(:test_command).to_s.parameterize)
end

def run_test

def run_test
  infom "Running test command \"#{::Shellwords.join(test_command_args)}\"" \
    " on \"#{git_absolute_path}\"..."
  result = ::EacRubyUtils::Ruby.on_clean_environment { test_command.execute }
  infom 'Test done'
  write_result_cache(result)
end

def stderr_cache

def stderr_cache
  root_cache.child('stderr')
end

def stdout_cache

def stdout_cache
  root_cache.child('stdout')
end

def successful?

def successful?
  successful
end

def successful_cache

def successful_cache
  root_cache.child('successful')
end

def successful_label

def successful_label
  successful?.to_s.send((successful? ? :green : :red))
end

def successful_uncached

def successful_uncached
  if options.fetch(:no_cache) || !successful_cache.stored?
    checkout_revision
    run_test
  end
  successful_cache.read == 'true'
end

def test_command

def test_command
  ::EacRubyUtils::Envs.local.command(*test_command_args).chdir(git.to_s)
end

def test_command_args

def test_command_args
  r = ::Shellwords.split(options.fetch(:test_command).to_s)
  return r if r.any?
  raise 'No command found'
end

def to_s

def to_s
  sha1
end

def write_result_cache(result)

def write_result_cache(result)
  stdout_cache.write(result[:stdout])
  stderr_cache.write(result[:stderr])
  successful_cache.write(result[:exit_code].zero? ? 'true' : 'false')
end