<commit_before>require 'fileutils'
require 'json'

require_relative '../spec_helper'

require OctocatalogDiff::Spec.require_path('/catalog')
require OctocatalogDiff::Spec.require_path('/catalog-util/fileresources')

describe OctocatalogDiff::CatalogUtil::FileResources do
  def catalog_from_fixture(path)
    OctocatalogDiff::Catalog.new(json: File.read(OctocatalogDiff::Spec.fixture_path(path)))
  end

  describe '#convert_file_resources' do
    before(:each) do
      @tmpdir = Dir.mktmpdir
      FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/tiny-repo/modules'), @tmpdir
      Dir.mkdir File.join(@tmpdir, 'environments')
      File.symlink @tmpdir, File.join(@tmpdir, 'environments', 'production')
    end

    after(:each) do
      FileUtils.remove_entry_secure @tmpdir if File.directory?(@tmpdir)
    end

    it 'should use compilation directory if environments/production is unavailable' do
      FileUtils.rm_f File.join(@tmpdir, 'environments', 'production')

      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
      expect(obj.resources.size).to eq(3), obj.resources.to_json
      expect(obj.resources[0]).to eq(resources_save[0]), obj.resources.to_json
      expect(obj.resources[1]).to eq(resources_save[1]), obj.resources.to_json
      expect(obj.resources[2]['parameters']['content']).to eq("foo\n"), obj.resources.to_json
      expect(obj.resources[2]['parameters'].key?('source')).to eq(false), obj.resources.to_json

      # Make sure the symlink isn't there, i.e. that we actually tested this
      expect(File.exist?(File.join(@tmpdir, 'environments', 'production'))).to eq(false)
    end

    it 'should not run at all if modules directory cannot be found' do
      module_dir = File.join(@tmpdir, 'modules')
      FileUtils.remove_entry_secure module_dir if File.directory?(module_dir)

      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
      expect(obj.resources.size).to eq(3), obj.resources.to_json
      expect(obj.resources[2]['parameters']['source']).to eq('puppet:///modules/test/tmp/foo')
      expect(obj.resources[2]['parameters'].key?('content')).to eq(false), obj.resources.to_json
    end

    it 'should modify array with convertible resources' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
      expect(obj.resources.size).to eq(3), obj.resources.to_json
      expect(obj.resources[0]).to eq(resources_save[0]), obj.resources.to_json
      expect(obj.resources[1]).to eq(resources_save[1]), obj.resources.to_json
      expect(obj.resources[2]['parameters']['content']).to eq("foo\n"), obj.resources.to_json
      expect(obj.resources[2]['parameters'].key?('source')).to eq(false), obj.resources.to_json
    end

    it 'should return unmodified array with no convertible resources' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      obj.resources[2]['parameters']['content'] = 'buzz'
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to eq(resources_save)
    end

    it 'should raise error if sourced file is not found' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      obj.resources[2]['parameters']['source'] = 'puppet:///modules/this/does/not/exist'

      # Perform test
      expect do
        OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      end.to raise_error(Errno::ENOENT, %r{Unable to find 'puppet:///modules/this/does/not/exist'})
    end

    it 'should return original if compilation_dir is not a string' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = %w(foo bar)
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to eq(resources_save)
    end

    it 'should return original if compilation_dir is empty string' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = ''
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to eq(resources_save)
    end
<commit_msg>Update test to match new error message wording<commit_after>require 'fileutils'
require 'json'

require_relative '../spec_helper'

require OctocatalogDiff::Spec.require_path('/catalog')
require OctocatalogDiff::Spec.require_path('/catalog-util/fileresources')

describe OctocatalogDiff::CatalogUtil::FileResources do
  def catalog_from_fixture(path)
    OctocatalogDiff::Catalog.new(json: File.read(OctocatalogDiff::Spec.fixture_path(path)))
  end

  describe '#convert_file_resources' do
    before(:each) do
      @tmpdir = Dir.mktmpdir
      FileUtils.cp_r OctocatalogDiff::Spec.fixture_path('repos/tiny-repo/modules'), @tmpdir
      Dir.mkdir File.join(@tmpdir, 'environments')
      File.symlink @tmpdir, File.join(@tmpdir, 'environments', 'production')
    end

    after(:each) do
      FileUtils.remove_entry_secure @tmpdir if File.directory?(@tmpdir)
    end

    it 'should use compilation directory if environments/production is unavailable' do
      FileUtils.rm_f File.join(@tmpdir, 'environments', 'production')

      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
      expect(obj.resources.size).to eq(3), obj.resources.to_json
      expect(obj.resources[0]).to eq(resources_save[0]), obj.resources.to_json
      expect(obj.resources[1]).to eq(resources_save[1]), obj.resources.to_json
      expect(obj.resources[2]['parameters']['content']).to eq("foo\n"), obj.resources.to_json
      expect(obj.resources[2]['parameters'].key?('source')).to eq(false), obj.resources.to_json

      # Make sure the symlink isn't there, i.e. that we actually tested this
      expect(File.exist?(File.join(@tmpdir, 'environments', 'production'))).to eq(false)
    end

    it 'should not run at all if modules directory cannot be found' do
      module_dir = File.join(@tmpdir, 'modules')
      FileUtils.remove_entry_secure module_dir if File.directory?(module_dir)

      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
      expect(obj.resources.size).to eq(3), obj.resources.to_json
      expect(obj.resources[2]['parameters']['source']).to eq('puppet:///modules/test/tmp/foo')
      expect(obj.resources[2]['parameters'].key?('content')).to eq(false), obj.resources.to_json
    end

    it 'should modify array with convertible resources' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to be_a_kind_of(Array), obj.resources.to_json
      expect(obj.resources.size).to eq(3), obj.resources.to_json
      expect(obj.resources[0]).to eq(resources_save[0]), obj.resources.to_json
      expect(obj.resources[1]).to eq(resources_save[1]), obj.resources.to_json
      expect(obj.resources[2]['parameters']['content']).to eq("foo\n"), obj.resources.to_json
      expect(obj.resources[2]['parameters'].key?('source')).to eq(false), obj.resources.to_json
    end

    it 'should return unmodified array with no convertible resources' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      obj.resources[2]['parameters']['content'] = 'buzz'
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to eq(resources_save)
    end

    it 'should raise error if sourced file is not found' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = @tmpdir
      obj.resources[2]['parameters']['source'] = 'puppet:///modules/this/does/not/exist'

      # Perform test
      expect do
        OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      end.to raise_error(Errno::ENOENT, %r{Unable to resolve 'puppet:///modules/this/does/not/exist'})
    end

    it 'should return original if compilation_dir is not a string' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = %w(foo bar)
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to eq(resources_save)
    end

    it 'should return original if compilation_dir is empty string' do
      # Set up test
      obj = catalog_from_fixture('catalogs/catalog-test-file-v4.json')
      obj.compilation_dir = ''
      resources_save = obj.resources.dup

      # Perform test
      OctocatalogDiff::CatalogUtil::FileResources.convert_file_resources(obj)
      expect(obj.resources).to eq(resources_save)
    end

{'fraction_non_alphanumeric': 0.12339137017411052, 'fraction_numerical': 0.004920514761544285, 'mean_word_length': 3.9112453531598512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5394935', 'n_tokens_mistral': 3269, 'n_tokens_neox': 3269, 'n_words': 601}