Class: Ander

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

Class Method Summary collapse

Class Method Details

.add_sample_spec_fileObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ander.rb', line 129

def self.add_sample_spec_file
  f = open('spec/sample_spec.rb', 'w')

  f.write  "require 'rails_helper'\n"

  f.write   "RSpec.describe 'Target Object' do\n"

  f.write   "  before(:all) do\n"
  f.write   "  # 'This gets executed before all test cases in current script'\n"
  f.write   "  end\n"

  f.write   "  before(:each) do\n"
  f.write   "  # 'This gets executed before each test cases in current script'\n"
  f.write   "  end\n"

  f.write   "  after(:all) do\n"
  f.write   "  # 'This gets executed after all test cases in current script'\n"
  f.write   "  end\n"

  f.write   "  after(:each) do\n"
  f.write   "  # 'This gets executed after each test cases in current script'\n"
  f.write   "  end\n"

  f.write   "  describe 'Title of test case:' do\n"
  f.write   "    context 'Context of your test case' do\n"
  f.write   "      let(:x) {\n"
  f.write   "        x = 'This gets executed upon calling for x'\n"
  f.write   "      }\n"
  f.write   "      it 'Expected result of your test case' do\n"
  f.write   "        expect(x).to eq('This gets executed upon calling for x')\n"
  f.write   "      end\n"
  f.write   "    end\n"
  f.write   "  end\n"
  f.write   "end\n"
  f.close
end

.add_to_gemfileObject



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
# File 'lib/ander.rb', line 11

def self.add_to_gemfile
  # Include rspec, rails_spec and simplecov in Gemfile
  puts "\n--Adding rspec, rspec-rails and simplecov to the Gemfile"
  # Check if any gems already exists in Gemfile
  rspec = false
  rspec_rails = false
  simplecov = false
  open('Gemfile', 'r') { |f|
    x = f.readlines
    x.each do |i|
      if i.include? "'rspec'"
        rspec = true
      end
      if i.include? "'rspec-rails'"
        rspec_rails = true
      end
      if i.include? "'simplecov'"
        simplecov = true
      end
    end
  }
  # add to Gemfile only for the missing gems
  unless rspec and rspec_rails and simplecov
    open('Gemfile', 'a') { |f|
      f.puts 'group :development, :test do'
      unless rspec
        puts '-Added rspec to Gemfile'
        f.puts "  gem 'rspec'"
      end
      unless rspec_rails
        puts '-Added rspec-rails to Gemfile'
        f.puts "  gem 'rspec-rails'"
      end
      unless simplecov
        puts 'Added simplecov to Gemfile'
        f.puts "  gem 'simplecov'"
      end
      f.puts 'end'
    }
  end

end

.bundle_installObject



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

def self.bundle_install
  # Bundle install
   puts "\n--Executing Bundle Install"
  system('bundle install')
end

.configure_dotrspecObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ander.rb', line 69

def self.configure_dotrspec
  # configure .rspec
  puts "\n--configuring rspec output format in .rspec"
  puts '-setting output format to progress and html'
  open('.rspec', 'w') { |f|
    f.puts '--color'
    f.puts '--require spec_helper'
    f.puts '--format progress'
    f.puts '--format html'
    f.puts '--out spec/rspec_results.html'
  }
end

.modify_spec_helperObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ander.rb', line 82

def self.modify_spec_helper
  puts "--Modifying spec_helper.rb"
  open('spec/spec_helper.rb', 'r'){ |f|
    simple_flag = false
    rspec_rails = false
    @contents = f.readlines
    @contents.each do |i|
      if i.include? "require 'simplecov'"
        simple_flag = true
      end
      if i.include? "require 'rspec/rails'"
        rspec_rails = true
      end
    end
    if simple_flag
      puts 'simplecov setting already exists in spec_helper.rb'
    else
      puts "-Adding require 'simplecov' to spec_helper.rb"
      @contents.each_with_index {|i, index|
        if i.include? "RSpec.configure do |config|\n"
          @contents[index] =
            i + "  require 'simplecov'\n"\
                 "  SimpleCov.start\n"
        end
      }
    end
    if rspec_rails
    puts 'rails environment setting already exists in spec_helper.rb'
    else rspec_rails
      puts "-Adding rails environment setting and require 'rspec/rails' to spec_helper.rb"
      @contents.each_with_index {|i, index|
        if i.include? "RSpec.configure do |config|\n"
          @contents[index] =
            i + "  ENV['RAILS_ENV'] = 'test'\n"\
                "  require File.expand_path('../../config/environment', __FILE__)\n"\
                "  require 'rspec/rails'\n"
        end
      }
    end
  }
  f = open('spec/spec_helper.rb', 'w')
  @contents.each do |i|
    f.write(i)
  end
  f.close
end

.run_setup_commandsObject



60
61
62
63
64
65
66
67
# File 'lib/ander.rb', line 60

def self.run_setup_commands
  # run all required setup commands for rspec
  puts "\n--Running setup commands for rspec"
  system('spring stop')
  puts '-Executing rails generate rspec:install'
  system('rails generate rspec:install')
  system('spring stop')
end

.setup_allObject



2
3
4
5
6
7
8
9
# File 'lib/ander.rb', line 2

def self.setup_all
  self.add_to_gemfile
  self.bundle_install
  self.run_setup_commands
  self.configure_dotrspec
  self.modify_spec_helper
  self.add_sample_spec_file
end