Module: AugeasSpec::Augparse
- Defined in:
- vendor_modules/augeasproviders_core/spec/lib/augeas_spec/augparse.rb
Instance Method Summary collapse
-
#augparse(file, lens, result = '?') ⇒ Object
Creates a simple test file, reads in a fixture (that's been modified by the provider) and runs augparse against the expected tree.
-
#augparse_filter(file, lens, filter, result) ⇒ Object
Takes a full fixture file, loads it in Augeas, uses the relative path and/or filter and saves just that part in a new file.
Instance Method Details
#augparse(file, lens, result = '?') ⇒ Object
Creates a simple test file, reads in a fixture (that's been modified by the provider) and runs augparse against the expected tree.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'vendor_modules/augeasproviders_core/spec/lib/augeas_spec/augparse.rb', line 9 def augparse(file, lens, result = '?') Dir.mktmpdir do |dir| # Augeas always starts with a blank line when creating new files, so # reprocess file and remove it to make writing tests easier File.open("#{dir}/input", 'w') do |finput| File.open(file, 'r') do |ffile| line = ffile.readline finput.write line unless line == "\n" ffile.each { |l| finput.write l } end end # Test module, Augeas reads back in the input file testaug = "#{dir}/test_augeasproviders.aug" File.open(testaug, 'w') do |tf| tf.write(<<~EOS) module Test_Augeasproviders = test #{lens} get Sys.read_file "#{dir}/input" = #{result} EOS end output = `augparse --notypecheck #{testaug} 2>&1` raise AugeasSpec::Error, "augparse failed:\n#{output}" unless $CHILD_STATUS == 0 && output.empty? end end |
#augparse_filter(file, lens, filter, result) ⇒ Object
Takes a full fixture file, loads it in Augeas, uses the relative path and/or filter and saves just that part in a new file. That's then passed into augparse and compared against the expected tree. Saves creating a tree of the entire file.
Because the filtered fragment is saved in a new file, seq labels will reset too, so it'll be “1” rather than what it was in the original fixture.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'vendor_modules/augeasproviders_core/spec/lib/augeas_spec/augparse.rb', line 43 def augparse_filter(file, lens, filter, result) # duplicate the original since we use aug.mv tmpin = Tempfile.new('original') tmpin.write(File.read(file)) tmpin.close tmpout = Tempfile.new('filtered') tmpout.close aug_open(tmpin.path, lens) do |aug| # Load a transform of the target, so Augeas can write into it aug.transform( lens: lens, name: lens.split('.')[0], incl: tmpout.path, excl: [] ) aug.load! tmpaug = "/files#{tmpout.path}" raise AugeasSpec::Error, "Augeas didn't load empty file #{tmpout.path}" if aug.match(tmpaug).empty? # Check the filter matches something and move it ftmatch = aug.match(filter) raise AugeasSpec::Error, "Filter #{filter} within #{file} matched #{ftmatch.size} nodes, should match at least one" if ftmatch.empty? loop do # Loop on aug_match as path indexes will change as we move nodes fp = ftmatch.first aug.mv(fp, "#{tmpaug}/#{fp.split(%r{/})[-1]}") ftmatch = aug.match(filter) break if ftmatch.empty? end aug.save! end augparse(tmpout.path, lens, result) ensure tmpin.unlink tmpout.unlink end |