|  | 
|  | 1 | +# spec/acceptance/deferred_spec.rb | 
|  | 2 | +# frozen_string_literal: true | 
|  | 3 | + | 
|  | 4 | +require 'spec_helper_acceptance' | 
|  | 5 | + | 
|  | 6 | +def read_fixture(name) | 
|  | 7 | +  File.read(File.join(__dir__, '..', 'fixtures', 'manifests', name)) | 
|  | 8 | +end | 
|  | 9 | + | 
|  | 10 | +def read_win_file_if_exists(path) | 
|  | 11 | +  # Use a script block with literals; avoid $variables to prevent transport/quoting expansion | 
|  | 12 | +  # Also keep exit 0 regardless of existence so run_shell doesn't raise. | 
|  | 13 | +  ps = %{& { if (Test-Path -LiteralPath '#{path}') { Get-Content -Raw -LiteralPath '#{path}' } else { '<<<FILE_NOT_FOUND>>>' } } } | 
|  | 14 | +  r  = run_shell(%(powershell.exe -NoProfile -NonInteractive -Command "#{ps}")) | 
|  | 15 | +  body = (r.stdout || '').to_s | 
|  | 16 | +  exists = !body.include?('<<<FILE_NOT_FOUND>>>') | 
|  | 17 | +  { exists: exists, content: exists ? body : '' } | 
|  | 18 | +end | 
|  | 19 | + | 
|  | 20 | +describe 'deferred values with dsc_lite' do | 
|  | 21 | +  let(:control_manifest)         { read_fixture('01_file_deferred.pp') } | 
|  | 22 | +  let(:dsc_deferred_direct)      { read_fixture('02_dsc_deferred_direct.pp') } | 
|  | 23 | +  let(:dsc_deferred_inline)      { read_fixture('02b_dsc_deferred_inline.pp') } # <— NEW | 
|  | 24 | +  let(:dsc_deferred_stringified) { read_fixture('03a_dsc_deferred_stringified.pp') } | 
|  | 25 | +  let(:dsc_deferred_bad_unwrap)  { read_fixture('03b_dsc_deferred_bad_unwrap.pp') } | 
|  | 26 | + | 
|  | 27 | +  it 'control (01): native file + Deferred resolves to hello-file' do | 
|  | 28 | +    result = idempotent_apply(control_manifest) | 
|  | 29 | +    expect(result.exit_code).to eq(0) | 
|  | 30 | +    out = read_win_file_if_exists('C:/Temp/deferred_ok.txt') | 
|  | 31 | +    expect(out[:exists]).to be(true) | 
|  | 32 | +    expect(out[:content].strip).to eq('hello-file') | 
|  | 33 | +  end | 
|  | 34 | + | 
|  | 35 | +  it '02: passing Deferred via variable to DSC resolves to hello-dsc (otherwise flag bug)' do | 
|  | 36 | +    apply = apply_manifest(dsc_deferred_direct) | 
|  | 37 | +    out   = read_win_file_if_exists('C:/Temp/from_dsc.txt') | 
|  | 38 | +    content = out[:content].strip | 
|  | 39 | +    if out[:exists] && content == 'hello-dsc' | 
|  | 40 | +      expect(true).to be(true) | 
|  | 41 | +    elsif out[:exists] && content =~ %r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i | 
|  | 42 | +      raise "BUG: 02 wrote stringified Deferred: #{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" | 
|  | 43 | +    else | 
|  | 44 | +      raise "Unexpected 02 outcome. Exists=#{out[:exists]} Content=#{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" | 
|  | 45 | +    end | 
|  | 46 | +  end | 
|  | 47 | + | 
|  | 48 | +  # NEW 02b: inline Deferred on the DSC property (no variable intermediary) | 
|  | 49 | +  it '02b: passing Deferred inline to DSC resolves to hello-dsc-inline (otherwise flag bug)' do | 
|  | 50 | +    apply = apply_manifest(dsc_deferred_inline) | 
|  | 51 | +    out   = read_win_file_if_exists('C:/Temp/from_dsc_inline.txt') | 
|  | 52 | +    content = out[:content].strip | 
|  | 53 | +    if out[:exists] && content == 'hello-dsc-inline' | 
|  | 54 | +      expect(true).to be(true) | 
|  | 55 | +    elsif out[:exists] && content =~ %r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i | 
|  | 56 | +      raise "BUG: 02b wrote stringified Deferred: #{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" | 
|  | 57 | +    else | 
|  | 58 | +      raise "Unexpected 02b outcome. Exists=#{out[:exists]} Content=#{content.inspect}\nApply:\n#{apply.stdout}#{apply.stderr}" | 
|  | 59 | +    end | 
|  | 60 | +  end | 
|  | 61 | + | 
|  | 62 | +  it '03a: stringifying a Deferred writes the function form (reproduces customer report)' do | 
|  | 63 | +    apply_manifest(dsc_deferred_stringified) | 
|  | 64 | +    out = read_win_file_if_exists('C:/Temp/from_dsc_var_string.txt') | 
|  | 65 | +    expect(out[:exists]).to be(true) | 
|  | 66 | +    expect(out[:content]).to match(%r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i) | 
|  | 67 | +    expect(out[:content]).not_to match(%r{\bhello-var\b}) | 
|  | 68 | +  end | 
|  | 69 | + | 
|  | 70 | +  it '03b: unwrap on a non‑Sensitive is a no‑op; also writes the function form' do | 
|  | 71 | +    apply_manifest(dsc_deferred_bad_unwrap) | 
|  | 72 | +    out   = read_win_file_if_exists('C:/Temp/from_dsc_var_bad_unwrap.txt') | 
|  | 73 | +    out   = read_win_file_if_exists('C:/Temp/from_dsc_var.txt') unless out[:exists] | 
|  | 74 | +    expect(out[:exists]).to be(true) | 
|  | 75 | +    expect(out[:content]).to match(%r{Deferred\s*\(|Puppet::Pops::Types::Deferred}i) | 
|  | 76 | +    expect(out[:content]).not_to match(%r{\bhello-var\b}) | 
|  | 77 | +  end | 
|  | 78 | +end | 
0 commit comments