<commit_before>require 'spec_helper_acceptance'

describe 'powershell provider:' do #, :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
  shared_examples 'should fail' do |manifest, error_check|
    it 'should throw an error' do
      expect { apply_manifest(manifest, :catch_failures => true, :future_parser => FUTURE_PARSER) }.to raise_error(error_check)
    end
  end

  shared_examples 'apply success' do |manifest|
    it 'should succeed' do
      apply_manifest(manifest, :catch_failures => true, :future_parser => FUTURE_PARSER)
    end
  end

  describe 'should run successfully' do

    p1 = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'Get-Process > c:/process.txt',
        unless    => 'if(!(test-path "c:/process.txt")){exit 1}',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      # Run it twice and test for idempotency
      apply_manifest(p1, :catch_failures => true, :future_parser => FUTURE_PARSER)
    end

    it 'should be idempotent' do
      expect(apply_manifest(p1, :catch_failures => true, :future_parser => FUTURE_PARSER).exit_code).to be_zero
    end

  end

  describe 'should run commands that exit session' do

    exit_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      apply_manifest(exit_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should run a second time' do
      apply_manifest(exit_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should run commands that break session' do

    break_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'Break',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      apply_manifest(break_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should run a second time' do
      apply_manifest(break_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should run commands that return from session' do

    return_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'return 0',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      apply_manifest(return_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should run a second time' do
      apply_manifest(return_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should not leak variables across calls to single session' do

    var_leak_setup_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => '$special=1',
        provider  => powershell,
      }
    MANIFEST

    var_leak_test_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'if ( $special -eq 1 ) { exit 1 } else { exit 0 }',
        provider  => powershell,
      }
    MANIFEST

    it 'should not see variable from previous run' do
      # Setup the variable
      apply_manifest(var_leak_setup_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)

      # Test to see if subsequent call sees the variable
      apply_manifest(var_leak_test_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should allow exit from unless' do

    unless_not_triggered_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        unless    => 'exit 1',
        provider  => powershell,
      }
    MANIFEST

    unless_triggered_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        unless    => 'exit 0',
        provider  => powershell,
      }
    MANIFEST

    it 'should RUN command if unless is NOT triggered' do
      apply_manifest(unless_not_triggered_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should NOT run command if unless IS triggered' do
      apply_manifest(unless_triggered_pp, :catch_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should be able to access the files after execution' do

    p2 = <<-MANIFEST
      exec{"TestPowershell":
        command   => 'Get-Service *puppet* | Out-File -FilePath C:/services.txt -Encoding UTF8',
        provider  => powershell
      }
    MANIFEST<commit_msg>(MODULES-3011) Add Tests for 'onlyif'<commit_after>require 'spec_helper_acceptance'

describe 'powershell provider:' do #, :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do
  shared_examples 'should fail' do |manifest, error_check|
    it 'should throw an error' do
      expect { apply_manifest(manifest, :catch_failures => true, :future_parser => FUTURE_PARSER) }.to raise_error(error_check)
    end
  end

  shared_examples 'apply success' do |manifest|
    it 'should succeed' do
      apply_manifest(manifest, :catch_failures => true, :future_parser => FUTURE_PARSER)
    end
  end

  describe 'should run successfully' do

    p1 = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'Get-Process > c:/process.txt',
        unless    => 'if(!(test-path "c:/process.txt")){exit 1}',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      # Run it twice and test for idempotency
      apply_manifest(p1, :catch_failures => true, :future_parser => FUTURE_PARSER)
    end

    it 'should be idempotent' do
      expect(apply_manifest(p1, :catch_failures => true, :future_parser => FUTURE_PARSER).exit_code).to be_zero
    end

  end

  describe 'should run commands that exit session' do

    exit_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      apply_manifest(exit_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should run a second time' do
      apply_manifest(exit_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should run commands that break session' do

    break_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'Break',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      apply_manifest(break_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should run a second time' do
      apply_manifest(break_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should run commands that return from session' do

    return_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'return 0',
        provider  => powershell,
      }
    MANIFEST

    it 'should not error on first run' do
      apply_manifest(return_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should run a second time' do
      apply_manifest(return_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should not leak variables across calls to single session' do

    var_leak_setup_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => '$special=1',
        provider  => powershell,
      }
    MANIFEST

    var_leak_test_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'if ( $special -eq 1 ) { exit 1 } else { exit 0 }',
        provider  => powershell,
      }
    MANIFEST

    it 'should not see variable from previous run' do
      # Setup the variable
      apply_manifest(var_leak_setup_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)

      # Test to see if subsequent call sees the variable
      apply_manifest(var_leak_test_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should allow exit from unless' do

    unless_not_triggered_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        unless    => 'exit 1',
        provider  => powershell,
      }
    MANIFEST

    unless_triggered_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        unless    => 'exit 0',
        provider  => powershell,
      }
    MANIFEST

    it 'should RUN command if unless is NOT triggered' do
      apply_manifest(unless_not_triggered_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should NOT run command if unless IS triggered' do
      apply_manifest(unless_triggered_pp, :catch_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should allow exit from onlyif' do

    onlyif_not_triggered_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        onlyif    => 'exit 1',
        provider  => powershell,
      }
    MANIFEST

    onlyif_triggered_pp = <<-MANIFEST
      exec{'TestPowershell':
        command   => 'exit 0',
        onlyif    => 'exit 0',
        provider  => powershell,
      }
    MANIFEST

    it 'should NOT run command if onlyif is NOT triggered' do
      apply_manifest(onlyif_not_triggered_pp, :catch_changes => true, :future_parser => FUTURE_PARSER)
    end

    it 'should RUN command if onlyif IS triggered' do
      apply_manifest(onlyif_triggered_pp, :expect_changes => true, :future_parser => FUTURE_PARSER)
    end

  end

  describe 'should be able to access the files after execution' do

    p2 = <<-MANIFEST
      exec{"TestPowershell":
        command   => 'Get-Service *puppet* | Out-File -FilePath C:/services.txt -Encoding UTF8',
        provider  => powershell
      }
    MANIFEST
{'fraction_non_alphanumeric': 0.09629936767907121, 'fraction_numerical': 0.004146366746138696, 'mean_word_length': 2.801418439716312, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 115, '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': '5436798', 'n_tokens_mistral': 3181, 'n_tokens_neox': 3008, 'n_words': 849}