spec/lib/augeas_spec/augparse.rb
# 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.
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.
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 .
def augparse_filter(file, lens, filter, result)
    tmpin = Tempfile.new('original')
    tmpin.write(File.read(file))
    tmpin.close
    tmpout = Tempfile.new('filtered')
    tmpout.close
    aug_open(tmpin.path, lens) do |aug|
      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?
      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
        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
def augparse_filter ( file , lens , filter , result ) tmpin = Tempfile . new ( 'original' ) tmpin . write ( File . read ( file ) ) tmpin . close tmpout = Tempfile . new ( 'filtered' ) tmpout . close aug_open ( tmpin . path , lens ) do | aug | 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? 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 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
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.
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 .
# duplicate the original since we use aug.mv # Load a transform of the target, so Augeas can write into it # Check the filter matches something and move it # Loop on aug_match as path indexes will change as we move nodes
file
<not_specific>
lens
<not_specific>
filter
<not_specific>
result
<not_specific>