# Inspec test for recipe irrigate-php::composer

# The Inspec reference, with examples and extensive documentation, can be
# found at http://inspec.io/docs/reference/resources/

control 'composer' do
  # For using node attributes in tests
  nodes_dir = '/tmp/kitchen/nodes'
  output = command("ls #{nodes_dir}").stdout.strip
  node_json = "#{nodes_dir}/#{output}"

  if file(node_json).exist?
    node = json(node_json).params

    describe file '/root/composer-install.sh' do
      it { should exist }
      its('group') { should eq 'root' }
      its('owner') { should eq 'root' }
      its('mode') { should cmp '0755' }
    end

    describe file '/usr/local/bin/composer' do
      it { should exist }
      it { should be_executable }
    end

    deploy_config_directories = %w(
      /.config
      /.config/composer
      /.composer
    )

    deploy_config_directories.each do |dir|
      describe directory node['default']['php']['deploy_home_dir'] + dir do
        it { should exist }
        its('owner') { should eq node['default']['php']['deploy_user'] }
        its('mode') { should cmp '0755' }
      end
    end

    [node['default']['php']['deploy_home_dir'] + '/.config/composer/auth.json',
     node['default']['php']['deploy_home_dir'] + '/.composer/auth.json'].each do |dest_file|
      describe file dest_file do
        it { should exist }
        its('owner') { should eq node['default']['php']['deploy_user'] }
        its('owner') { should eq node['default']['php']['deploy_user'] }
      end
    end

  else
    puts "File #{node_json} does not exist, skiping attribute-depending checks"
  end
end
