File: executable_test.rb

package info (click to toggle)
ruby-whenever 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 328 kB
  • sloc: ruby: 2,722; makefile: 2
file content (142 lines) | stat: -rw-r--r-- 3,741 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'test_helper'

describe 'Executable' do
  describe 'bin/wheneverize' do
    describe 'ARGV is not empty' do
      describe 'file does not exist' do
        file = '/tmp/this_does_not_exist'

        it 'prints STDERR' do
          out, err = capture_subprocess_io do
            system('wheneverize', file)
          end

          assert_empty(out)
          assert_match(/`#{file}' does not exist./, err)
        end
      end

      describe 'file exists, but not a directory' do
        file = '/tmp/this_is_a_file.txt'
        before { FileUtils.touch(file) }

        it 'prints STDERR' do
          begin
            out, err = capture_subprocess_io do
              system('wheneverize', file)
            end

            assert_empty(out)
            assert_match(/`#{file}' is not a directory./, err)
          ensure
            FileUtils.rm(file)
          end
        end
      end

      describe 'file is a directory, but another param(s) are given as well' do
        file = '/tmp/this_is_a_directory'
        before { FileUtils.mkdir(file) }

        it 'prints STDERR' do
          begin
            out, err = capture_subprocess_io do
              system('wheneverize', file, 'another', 'parameters')
            end

            assert_empty(out)
            assert_match(/#{"Too many arguments; please specify only the " \
                         "directory to wheneverize."}/, err)
          ensure
            FileUtils.rmdir(file)
          end
        end
      end
    end

    describe 'ARGV is empty' do
      dir  = '.'
      file = 'config/schedule.rb'
      path = File.join(dir, file)

      describe 'config file already exists' do
        before do
          FileUtils.mkdir(File.dirname(path))
          FileUtils.touch(path)
        end

        it 'prints STDOUT and STDERR' do
          begin
            out, err = capture_subprocess_io do
              system('wheneverize')
            end

            assert_match(/\[done\] wheneverized!/, out)
            assert_match(/\[skip\] `#{path}' already exists/, err)
          ensure
            FileUtils.rm_rf(File.dirname(path))
          end
        end
      end

      describe 'config directory does not exist' do
        it 'prints STDOUT and STDERR' do
          begin
            out, err = capture_subprocess_io do
              system('wheneverize')
            end

            assert_match(/\[add\] creating `#{File.dirname(path)}'\n/, err)
            assert_match(/\[done\] wheneverized!/, out)
          ensure
            FileUtils.rm_rf(File.dirname(path))
          end
        end
      end

      describe 'config directory exists, but file does not' do
        before { FileUtils.mkdir(File.dirname(path)) }

        it 'writes config file and prints STDOUT' do
          begin
            out, err = capture_subprocess_io do
              system('wheneverize')
            end

            assert_empty(err)
            assert_match(
              /\[add\] writing `#{path}'\n\[done\] wheneverized!/,
              out
            )

            assert_match((<<-FILE
# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron

# Example:
#
# set :output, "/path/to/my/cron_log.log"
#
# every 2.hours do
#   command "/usr/bin/some_great_command"
#   runner "MyModel.some_method"
#   rake "some:great:rake:task"
# end
#
# every 4.days do
#   runner "AnotherModel.prune_old_records"
# end

# Learn more: http://github.com/javan/whenever
FILE
                         ), IO.read(path))
          ensure
            FileUtils.rm_rf(File.dirname(path))
          end
        end
      end
    end
  end
end