File: executable_mock.rb

package info (click to toggle)
ruby-cliver 0.3.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: ruby: 760; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,037 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
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
35
36
37
38
39
40
41
42
43
44
45
# encoding: utf-8

class ExecutableMock
  def initialize(path)
    @path = _to_platform_abs_path(path)
  end
  attr_reader :path
  attr_reader :version

  def == (other_path)
    other_path = _to_platform_abs_path(other_path)
    if other_path[/[A-Z]:/i] # windows
      pattern = /\A#{self.path}(#{(ENV['PATHEXT']||'').split(';').map(&Regexp::method(:escape)).join('|')})?\Z/i
      pattern =~ other_path
    else # posix
      self.path == other_path
    end
  end

  private

  def _to_platform_abs_path(source)
    (File::absolute_path?(source, :windows) && !File::absolute_path?(source, :posix)) ?
      source.tr('\\','/') :
      File.expand_path(source)
  end

  class Registry
    def initialize(version_map)
      @registry = {}
      version_map.each do |path,version|
        @registry[ExecutableMock.new(path)] = version
      end
    end

    def executable?(path)
      false | version(path)
    end

    def version(path)
      key = @registry.keys.find {|exe| exe == path }
      key && @registry[key]
    end
  end
end