File: rack_test.rb

package info (click to toggle)
ruby-prof 0.16.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,680 kB
  • ctags: 972
  • sloc: ruby: 4,552; ansic: 1,888; makefile: 6
file content (93 lines) | stat: -rwxr-xr-x 2,288 bytes parent folder | download
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
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
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env ruby
# encoding: UTF-8

require File.expand_path('../test_helper', __FILE__)

class FakeRackApp
  def call(env)
  end
end

module Rack
  class Request
    def initialize(env)
      if env == :fake_env
        @env = {}
      else
        @env = env
      end
    end

    def path
      @env[:path] || '/path/to/resource.json'
    end
  end
end

class RackTest < TestCase
  def test_create_print_path
    path = Dir.mktmpdir
    Dir.delete(path)

    Rack::RubyProf.new(FakeRackApp.new, :path => path)

    assert(Dir.exist?(path))
  end

  def test_create_profile_reports
    path = Dir.mktmpdir

    adapter = Rack::RubyProf.new(FakeRackApp.new, :path => path)

    adapter.call(:fake_env)

    %w(flat.txt graph.txt graph.html call_stack.html).each do |base_name|
      file_path = ::File.join(path, "path-to-resource.json-#{base_name}")
      assert(File.exist?(file_path))
    end
  end

  def test_skip_paths
    path = Dir.mktmpdir

    adapter = Rack::RubyProf.new(FakeRackApp.new, :path => path, :skip_paths => [%r{\.json$}])

    adapter.call(:fake_env)

    %w(flat.txt graph.txt graph.html call_stack.html).each do |base_name|
      file_path = ::File.join(path, "path-to-resource.json-#{base_name}")
      assert(!File.exist?(file_path))
    end
  end

  def test_only_paths
    path = Dir.mktmpdir

    adapter = Rack::RubyProf.new(FakeRackApp.new, :path => path, :only_paths => [%r{\.json$}])

    adapter.call({path: '/path/to/resource.json'})

    %w(flat.txt graph.txt graph.html call_stack.html).each do |base_name|
      file_path = ::File.join(path, "path-to-resource.json-#{base_name}")
      assert(File.exist?(file_path))
    end

    adapter.call({path: '/path/to/resource.html'})
    %w(flat.txt graph.txt graph.html call_stack.html).each do |base_name|
      file_path = ::File.join(path, "path-to-resource.html-#{base_name}")
      assert(!File.exist?(file_path))
    end
  end

  def test_allows_lazy_filename_setting
    path = Dir.mktmpdir

    printer = {::RubyProf::FlatPrinter => lambda { 'dynamic.txt' }}
    adapter = Rack::RubyProf.new(FakeRackApp.new, :path => path, :printers => printer)

    adapter.call(:fake_env)

    file_path = ::File.join(path, 'path-to-resource.json-dynamic.txt')
    assert(File.exist?(file_path))
  end
end