summaryrefslogtreecommitdiff
path: root/test/rake/test_rake_cpu_counter.rb
blob: ccf21d8ba6a206570669c2ab379ccddd96951e68 (plain)
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
46
47
48
49
50
require File.expand_path('../helper', __FILE__)

class TestRakeCpuCounter < Rake::TestCase

  def setup
    super

    @cpu_counter = Rake::CpuCounter.new
  end

  def test_count_via_win32
    if Rake::Win32.windows? then
      assert_kind_of Numeric, @cpu_counter.count_via_win32
    else
      assert_nil @cpu_counter.count_via_win32
    end
  end

  def test_in_path_command
    with_ruby_in_path do |ruby|
      assert_equal ruby, @cpu_counter.in_path_command(ruby)
    end
  rescue Errno::ENOENT => e
    raise unless e.message =~ /\bwhich\b/

    skip 'cannot find which for this test'
  end

  def test_run
    with_ruby_in_path do |ruby|
      assert_equal 7, @cpu_counter.run(ruby, '-e', 'puts 3 + 4')
    end
  end

  def with_ruby_in_path
    ruby     = File.basename Gem.ruby
    ruby_dir = File.dirname  Gem.ruby

    begin
      orig_path, ENV['PATH'] =
        ENV['PATH'], [ruby_dir, *ENV['PATH']].join(File::PATH_SEPARATOR)

      yield ruby
    ensure
      ENV['PATH'] = orig_path
    end
  end

end