title "Cinc omnibus-toolchain installed binaries"

install_dir = os.family == "windows" ? "C:/cinc-project/omnibus-toolchain" : "/opt/omnibus-toolchain"
# Test the public-facing wrappers in bin/ (created by omnibus's
# appbundle step) — that's the surface users actually invoke. Falling
# back to embedded/bin/ for a binary that exists in bin/ would mask a
# broken wrapper.
#
# On Windows nothing is symlinked from embedded/bin/ to bin/ (Windows
# does not have the same symlink semantics), so only appbundle-managed
# entries land in bin/. Ruby, bundle, etc. live solely in embedded/bin/
# and the Windows control below points there.
bin = "#{install_dir}/bin"
embedded_bin = "#{install_dir}/embedded/bin"

control "Install layout" do
  impact 1.0
  title "Install dir present with version manifest"
  desc "The package must lay down install_dir and version-manifest.txt."

  describe directory(install_dir) do
    it { should exist }
  end

  describe file("#{install_dir}/version-manifest.txt") do
    it { should exist }
    its("content") { should match(/^omnibus-toolchain /) }
  end
end

control "Toolchain binaries on *nix" do
  impact 1.0
  title "GNU userland and Ruby work"
  desc "Common build-toolchain commands must execute successfully."
  only_if { os.family != "windows" }

  %W{
    #{bin}/bash
    #{bin}/make
    #{bin}/ruby
    #{bin}/gem
    #{bin}/bundle
    #{bin}/curl
  }.each do |b|
    describe command("#{b} --version") do
      its("exit_status") { should eq 0 }
    end
  end

  describe command("#{bin}/tar --version") do
    its("exit_status") { should eq 0 }
    # GNU tar is required on every supported platform
    its("stdout") { should match(/GNU tar/) }
  end

  describe command("#{bin}/cinc-auditor version") do
    its("exit_status") { should eq 0 }
  end
end

control "Berkshelf on Linux" do
  impact 1.0
  title "berks is bundled on Linux"
  desc "berkshelf is only a dependency on Linux (chef-server and its " \
       "components never built on macOS/FreeBSD), so only assert it there."
  only_if { os.linux? }

  describe command("#{bin}/berks --version") do
    its("exit_status") { should eq 0 }
  end
end

control "Toolchain binaries on Windows" do
  impact 1.0
  title "Ruby toolchain works on Windows"
  desc "Windows builders need at least Ruby and the public wrappers. " \
       "Ruby and bundle live in embedded/bin/ on Windows because " \
       "embedded-to-bin symlinking is a no-op there; only appbundle " \
       "entries (cinc-auditor) end up in bin/."
  only_if { os.family == "windows" }

  describe command("#{embedded_bin}/ruby.exe --version") do
    its("exit_status") { should eq 0 }
  end

  describe command("#{embedded_bin}/bundle.bat --version") do
    its("exit_status") { should eq 0 }
  end

  describe command("#{bin}/cinc-auditor.bat version") do
    its("exit_status") { should eq 0 }
  end
end

control "No Chef branding in install_dir" do
  impact 0.5
  title "Cinc-installed paths must not reference Chef Software identifiers"
  desc "Catch accidental upstream-style paths slipping back in (e.g. /opt/chef)."

  describe directory("/opt/chef") do
    it { should_not exist }
  end

  if os.family == "windows"
    describe directory("C:/opscode/omnibus-toolchain") do
      it { should_not exist }
    end
  end
end
