Sha256: c581e834b4b0fc7de4d3b18b3bb6e6a20e8eff2c5698701d3a699359dc8c1105

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module ArubaDoubles
  module Api
    def doubled?
      !@doubles_dir.nil?
    end
    
    def create_double(file, options = {})
      unless doubled?
        create_doubles_dir
        patch_original_path
      end
      write_double(file, options)
    end
    
    def remove_doubles
      restore_original_path
      remove_doubles_dir
    end

  private

    def create_doubles_dir
      @doubles_dir = Dir.mktmpdir
    end

    def patch_original_path
      @__aruba_doubles_original_path = (ENV['PATH'] || '').split(File::PATH_SEPARATOR)
      ENV['PATH'] = ([@doubles_dir] + @__aruba_doubles_original_path).join(File::PATH_SEPARATOR)
    end

    def write_double(command_line, options = {})
      args = command_line.split
      filename = args.shift
      double = File.expand_path(filename, @doubles_dir)
      File.open(double, 'w') do |f|
        f.puts "#!/usr/bin/env ruby"
        f.puts "# Doubled command line application by aruba-doubles\n"
        f.puts "unless ARGV.to_s == \"#{args}\""
        f.puts "  warn \"expected: #{filename} #{args}\""
        f.puts "  warn \"     got: #{filename} \#{ARGV}\""
        f.puts "  exit(1)"
        f.puts "end"
        f.puts "puts ([File.basename(__FILE__)] + ARGV).join(' ')" if @repeat_arguments
        f.puts "puts \"#{options[:stdout]}\"" if options[:stdout]
        f.puts "warn \"#{options[:stderr]}\"" if options[:stderr]
        f.puts "exit(#{options[:exit_status]})" if options[:exit_status]
      end
      FileUtils.chmod(0755, double)
    end

    def restore_original_path
      ENV['PATH'] = @__aruba_doubles_original_path.join(File::PATH_SEPARATOR) unless doubled?
    end

    def remove_doubles_dir
      FileUtils.rm_r(@doubles_dir) unless @doubles_dir.nil?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aruba-doubles-0.1.0 lib/aruba-doubles/api.rb