Class: Doubleshot::CLI::Commands::Test

Inherits:
Doubleshot::CLI show all
Defined in:
lib/doubleshot/commands/test.rb

Constant Summary

Constants inherited from Doubleshot::CLI

USAGE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Doubleshot::CLI

commands, detect, inherited, task_name, usage

Constructor Details

#initialize(config, ci_test, force_tests) ⇒ Test

Returns a new instance of Test.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/doubleshot/commands/test.rb', line 82

def initialize(config, ci_test, force_tests)
  @config = config
  @interrupted = false
  @ci_test = ci_test
  @force_tests = force_tests

  # Hit Ctrl-C once to re-run all specs; twice to exit the program.
  Signal.trap("INT") do
    if @interrupted
      puts "\nShutting down..."

      unless @force_tests
        @@pid_file.delete if @@pid_file.exist?
      end

      exit 0
    else
      @interrupted = true
      run_all_specs
      @interrupted = false
    end
  end
end

Class Method Details

.optionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/doubleshot/commands/test.rb', line 12

def self.options
  Options.new do |options|
    options.banner = "Usage: doubleshot test [options]"

    options.separator ""
    options.separator "Options:"

    options.ci_test = false
    options.on "--ci", "Run all tests, then exit. (No continuous listening for file changes.)" do
      options.ci_test = true
    end

    options.force_tests = false
    options.on "--force", "Run tests, even if doubleshot_test.pid exists." do
      options.force_tests = true
    end

    options.separator ""
    options.separator "Summary: #{summary}"
  end
end

.start(args) ⇒ Object



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
# File 'lib/doubleshot/commands/test.rb', line 34

def self.start(args)
  options = self.options.parse!(args)

  @@pid_file = Pathname(".doubleshot_test.pid")
  if @@pid_file.exist? && !options.force_tests

    begin
      if Process.getpgid(@@pid_file.read.to_i)
        puts ".doubleshot_test.pid exists: Are you running tests elsewhere? (Use --force to override.)"
        exit 1
      end
    rescue Errno::ESRCH
      @@pid_file.delete
    end
  end

  @@pid_file.open("w") do |pid|
    pid << $$
  end

  doubleshot = Doubleshot::current

  if Pathname::glob(doubleshot.config.source.tests + "**/*_{spec,test}.rb").empty?
    puts "No tests found"
    return 0
  end

  if options.ci_test
    if doubleshot.lockfile.exist?
      puts "--ci: Removing lockfile"
      doubleshot.lockfile.delete
    end

    if doubleshot.classpath_cache.exist?
      puts "--ci: Removing .classpath.cache"
      doubleshot.classpath_cache.delete
    end

    if doubleshot.config.target.exist?
      puts "--ci: Removing target build directory"
      doubleshot.config.target.rmtree
    end
  end

  watcher = new(doubleshot.config, options.ci_test, options.force_tests)
  watcher.run
end

.summaryObject



2
3
4
5
6
7
8
9
10
# File 'lib/doubleshot/commands/test.rb', line 2

def self.summary
  <<-EOS.margin
    A test harness that watches files, builds your
    source, and executes tests based on filename
    conventions. The location of your tests is
    determined by the 'config.source.tests'
    attribute of your Doubleshot configuration.
  EOS
end

Instance Method Details

#runObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/doubleshot/commands/test.rb', line 106

def run
  if @ci_test
    if @config.project == "doubleshot"
      Doubleshot::current.bootstrap!
      Doubleshot::current.build! false
    end
    exit_status = run_all_specs
    unless @force_tests
      @@pid_file.delete if @@pid_file.exist?
    end
    exit_status
  else
    # Output here just so you know when changes will be
    # picked up after you start the program.
    puts "Listening for changes..."
    listener.start
  end
end