<commit_before>#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/provider/package/pkgng'

provider_class = Puppet::Type.type(:package).provider(:pkgng)

describe provider_class do
  let(:name) { 'bash' }
  let(:pkgng) { 'pkgng' }

  let(:resource) do
    # When bash is not present
    Puppet::Type.type(:package).new(:name => name, :provider => pkgng)
  end

  let(:installed_resource) do
    # When zsh is present
    Puppet::Type.type(:package).new(:name => 'zsh', :provider => pkgng)
  end

  let(:latest_resource) do
    # When curl is installed but not the latest
    Puppet::Type.type(:package).new(:name => 'ftp/curl', :provider => pkgng, :ensure => latest)
  end

  let (:provider) { resource.provider }

  def run_in_catalog(*resources)
    catalog = Puppet::Resource::Catalog.new
    catalog.host_config = false
    resources.each do |resource|
      #resource.expects(:err).never
      catalog.add_resource(resource)
    end
    catalog.apply
  end

  before do
    provider_class.stub(:command).with(:pkg) { '/usr/local/sbin/pkg' }
    #provider.stub(:command).with(:pkg) {'/usr/local/sbin/pkg'}

    info = File.read('spec/fixtures/pkg.info')
    provider_class.stub(:get_query) { info }

    version_list = File.read('spec/fixtures/pkg.version')
    provider_class.stub(:get_version_list) { version_list }
  end

  context "#instances" do
    it "should return the empty set if no packages are listed" do
      provider_class.stub(:get_query) { '' }
      provider_class.stub(:get_version_list) { '' }
      provider_class.instances.should be_empty
    end

    it "should return all packages when invoked" do
      provider_class.instances.map(&:name).sort.should ==
        %w{ca_root_nss curl nmap pkg gnupg mcollective zsh tac_plus}.sort
    end

    it "should set latest to current version when no upgrade available" do
      nmap = provider_class.instances.find {|i| i.properties[:origin] == 'security/nmap' }

      nmap.properties[:version].should == nmap.properties[:latest]
    end

    describe "version" do
      it "should retrieve the correct version of the current package" do
        zsh = provider_class.instances.find {|i| i.properties[:origin] == 'shells/zsh' }
        zsh.properties[:version].should == '5.0.2_1'
      end
    end
  end

  context "#install" do
    it "should call pkg with the specified package version" do
      resource = Puppet::Type.type(:package).new(
        :name     => 'curl',
        :provider => :pkgng,
        :ensure   => '7.33.1'
      )
      resource.provider.should_receive(:pkg) do |arg|
        arg.should include('curl-7.33.1')
      end
      resource.provider.install
    end

    it "should call pkg with the specified package repo" do
      resource = Puppet::Type.type(:package).new(
        :name     => 'curl',
        :provider => :pkgng,
        :source   => 'urn:freebsd:repo:FreeBSD',
      )
      resource.provider.should_receive(:pkg) do |arg|
        arg.should include('FreeBSD')
      end
      resource.provider.install
    end
  end

  context "#query" do
    # This is being commented out as I am not sure how to test the code when
    # using prefetching.  I somehow need to pass a fake resources object into
    # #prefetch so that it can build the @property_hash, but I am not sure how.
    #
    #it "should return the installed version if present" do
    #  fixture = File.read('spec/fixtures/pkg.query')
    #  provider_class.stub(:get_resource_info) { fixture }
    #  resource[:name] = 'zsh'
    #  expect(provider.query).to eq({:version=>'5.0.2'})<commit_msg>fixed typo<commit_after>#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/provider/package/pkgng'

provider_class = Puppet::Type.type(:package).provider(:pkgng)

describe provider_class do
  let(:name) { 'bash' }
  let(:pkgng) { 'pkgng' }

  let(:resource) do
    # When bash is not present
    Puppet::Type.type(:package).new(:name => name, :provider => pkgng)
  end

  let(:installed_resource) do
    # When zsh is present
    Puppet::Type.type(:package).new(:name => 'zsh', :provider => pkgng)
  end

  let(:latest_resource) do
    # When curl is installed but not the latest
    Puppet::Type.type(:package).new(:name => 'ftp/curl', :provider => pkgng, :ensure => latest)
  end

  let (:provider) { resource.provider }

  def run_in_catalog(*resources)
    catalog = Puppet::Resource::Catalog.new
    catalog.host_config = false
    resources.each do |resource|
      #resource.expects(:err).never
      catalog.add_resource(resource)
    end
    catalog.apply
  end

  before do
    provider_class.stub(:command).with(:pkg) { '/usr/local/sbin/pkg' }
    #provider.stub(:command).with(:pkg) {'/usr/local/sbin/pkg'}

    info = File.read('spec/fixtures/pkg.info')
    provider_class.stub(:get_query) { info }

    version_list = File.read('spec/fixtures/pkg.version')
    provider_class.stub(:get_version_list) { version_list }
  end

  context "#instances" do
    it "should return the empty set if no packages are listed" do
      provider_class.stub(:get_query) { '' }
      provider_class.stub(:get_version_list) { '' }
      provider_class.instances.should be_empty
    end

    it "should return all packages when invoked" do
      provider_class.instances.map(&:name).sort.should ==
        %w{ca_root_nss curl nmap pkg gnupg mcollective zsh tac_plus}.sort
    end

    it "should set latest to current version when no upgrade available" do
      nmap = provider_class.instances.find {|i| i.properties[:origin] == 'security/nmap' }

      nmap.properties[:version].should == nmap.properties[:latest]
    end

    describe "version" do
      it "should retrieve the correct version of the current package" do
        zsh = provider_class.instances.find {|i| i.properties[:origin] == 'shells/zsh' }
        zsh.properties[:version].should == '5.0.2_1'
      end
    end
  end

  context "#install" do
    it "should call pkg with the specified package version" do
      resource = Puppet::Type.type(:package).new(
        :name     => 'curl',
        :provider => :pkgng,
        :ensure   => '7.33.1'
      )
      resource.provider.should_receive(:pkg) do |arg|
        arg.should include('curl-7.33.1')
      end
      resource.provider.install
    end

    it "should call pkg with the specified package repo" do
      resource = Puppet::Type.type(:package).new(
        :name     => 'curl',
        :provider => :pkgng,
        :source   => 'urn:freebsd:repo:FreeBSD'
      )
      resource.provider.should_receive(:pkg) do |arg|
        arg.should include('FreeBSD')
      end
      resource.provider.install
    end
  end

  context "#query" do
    # This is being commented out as I am not sure how to test the code when
    # using prefetching.  I somehow need to pass a fake resources object into
    # #prefetch so that it can build the @property_hash, but I am not sure how.
    #
    #it "should return the installed version if present" do
    #  fixture = File.read('spec/fixtures/pkg.query')
    #  provider_class.stub(:get_resource_info) { fixture }
    #  resource[:name] = 'zsh'
    #  expect(provider.query).to eq({:version=>'5.0.2'})
{'fraction_non_alphanumeric': 0.12643517222066647, 'fraction_numerical': 0.004200504060487258, 'mean_word_length': 3.0493197278911564, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '117454', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2250, 'n_words': 576}