Class: Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/inspector.rb

Overview

The inspector make some assumptions about how your project is structured and where your spec files are located. That said: The ‘spec’ directory, containing all your test files, must rest in the root directory of your project. Futhermore it tries to locate controller, model, helper and view specs for a rails app (or projects with an identical structure) in root/spec/controllers, root/spec/models, root/spec/helpers and root/spec/views.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_spec_rootObject

Returns the value of attribute base_spec_root.



8
9
10
# File 'lib/inspector.rb', line 8

def base_spec_root
  @base_spec_root
end

Class Method Details

.file_is_invalid?(file) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/inspector.rb', line 10

def self.file_is_invalid?(file)
  return true unless File.basename(file) =~ /.rb\z|.rhtml\z|.erb\z|.haml\z/
  false
end

Instance Method Details

#adapt_rails_specific_app_structure(location) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/inspector.rb', line 69

def adapt_rails_specific_app_structure(location)
  # Removing 'app' if its a rails controller, model, helper or view
  fu = location.split("/")
  if fu[1] == "app" && (fu[2] == 'controllers' || fu[2] == 'helpers' || fu[2] == 'models' || fu[2] == 'views')
    return "/" + fu[2..fu.length].join("/")
  end
  location
end

#append_spec_file_extension(spec_file) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/inspector.rb', line 78

def append_spec_file_extension(spec_file)
  if File.extname(spec_file) == ".rb"
    return File.join(File.dirname(spec_file), File.basename(spec_file, ".rb")) + "_spec.rb"
  else
    return spec_file + "_spec.rb"
  end
end

#extract_inner_project_location(file, spec_root) ⇒ Object



64
65
66
67
# File 'lib/inspector.rb', line 64

def extract_inner_project_location(file, spec_root)
  location = file.sub(project_root(spec_root), "")
  adapt_rails_specific_app_structure(location)
end

#file_is_a_spec?(file) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'lib/inspector.rb', line 86

def file_is_a_spec?(file)
  if file.split("/").include?('spec') && File.basename(file).match(/_spec.rb\z/)
    return true
  end
  false
end

#find_base_spec_root_by_file(file) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/inspector.rb', line 39

def find_base_spec_root_by_file(file)
  if @base_spec_root
    return @base_spec_root
  else
    dir_parts = File.dirname(file).split("/")
    dir_parts.size.times do |i|
      search_dir = dir_parts[0..dir_parts.length - i - 1].join("/") + "/"
      if Dir.entries(search_dir).include?('spec')
        @assumed_spec_root = search_dir + "spec" 
        break
      end
    end
    return @assumed_spec_root
  end
end

#find_spec_file(file) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/inspector.rb', line 15

def find_spec_file(file)
  begin
    return file if file_is_a_spec?(file)
    spec_root = find_base_spec_root_by_file(file)
    if spec_root
      guessed_spec_location = guess_spec_location(file, spec_root)
      if File.exist?(guessed_spec_location)
        @base_spec_root = spec_root
        return guessed_spec_location
      end
    end
    nil      
  rescue => e
    puts "Error while parsing a file: '#{file}'"
    puts e
  end
end

#guess_spec_location(file, spec_root) ⇒ Object



55
56
57
58
# File 'lib/inspector.rb', line 55

def guess_spec_location(file, spec_root)
  inner_location = extract_inner_project_location(file, spec_root)
  append_spec_file_extension(File.join(spec_root, inner_location))
end

#inner_spec_directory(path) ⇒ Object



33
34
35
36
37
# File 'lib/inspector.rb', line 33

def inner_spec_directory(path)
  spec_base_root = find_base_spec_root_by_file(Dir.pwd + "/.")
  inner_location = extract_inner_project_location(Dir.pwd, spec_base_root)
  File.join(spec_base_root, inner_location)
end

#project_root(spec_root) ⇒ Object



60
61
62
# File 'lib/inspector.rb', line 60

def project_root(spec_root)
  spec_root.split("/")[0...-1].join("/")
end