git_test 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ module GitTest
2
+ class Notify
3
+ class TextFormat
4
+ attr_accessor :length, :msg, :filler
5
+ ENGINE = "#=="
6
+ CABOOSE = "==#"
7
+ FILL_CHAR = "="
8
+
9
+ def initialize(msg, length)
10
+ self.msg = " #{msg} "
11
+ self.length = length
12
+ end
13
+
14
+ def filler_length_needed
15
+ length - (ENGINE.length + CABOOSE.length + msg.length)
16
+ end
17
+
18
+ def filler_per_side
19
+ filler_length_needed/2.0
20
+ end
21
+
22
+ def odd?
23
+ !even?
24
+ end
25
+
26
+ def even?
27
+ filler_per_side % 2 == 0
28
+ end
29
+
30
+ def message_too_long?
31
+ filler_length_needed < 0
32
+ end
33
+
34
+ # def truncate_message!
35
+ # self.msg # = msg[0, msg.length + filler_length_needed - 3 ] << "..."
36
+ # end
37
+
38
+ def add_filler!
39
+ filler = filler_per_side.floor.times.map {FILL_CHAR}.join("")
40
+ # result = if even?
41
+ # self.msg = "#{filler}#{msg}#{filler}"
42
+ # else
43
+ # self.msg = "#{filler}#{msg}#{filler}#{FILL_CHAR}"
44
+ # end
45
+ end
46
+
47
+ def output
48
+ "#{ENGINE}#{msg}"
49
+ end
50
+
51
+ def normalize
52
+ add_filler!
53
+ output
54
+ end
55
+
56
+ end
57
+ attr_accessor :output
58
+
59
+ def initialize(output = STDOUT)
60
+ self.output = output
61
+ end
62
+
63
+ def start(msg)
64
+ write(msg, :light_blue)
65
+ end
66
+
67
+ def sentiment(msg, bool = true)
68
+ write(msg, bool ? :green : :red)
69
+ end
70
+
71
+ def done(msg, passed = true)
72
+ sentiment(msg, passed)
73
+ end
74
+
75
+ def warn(msg)
76
+ write("WARNING: #{msg}", :red)
77
+ end
78
+
79
+ def critical_error(msg, exit_code = -1)
80
+ write(msg, :red)
81
+ exit! exit_code
82
+ end
83
+
84
+ def raw(msg)
85
+ output.puts(msg)
86
+ end
87
+
88
+ def write(msg, color = :light_yellow, truncate = 80)
89
+ msg = TextFormat.new(msg, truncate).normalize
90
+ output.puts "#{msg}".send color
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,49 @@
1
+ module GitTest
2
+ class Proj < Git::Base
3
+ attr_accessor :notify
4
+ def initialize(options = {}, notify = Notify.new)
5
+ self.notify = notify
6
+ path = options[:path]||"."
7
+ super :working_directory => path
8
+ end
9
+
10
+ def username
11
+ config["user.name"]
12
+ end
13
+
14
+ # add
15
+ # commit("this is my commit message")
16
+
17
+
18
+ def show(branch, file)
19
+ self.lib.send :command, "show", "#{branch}:#{file}"
20
+ end
21
+
22
+
23
+ def real_pull(remote = 'origin', branch = 'master')
24
+ check_repo_status!
25
+ self.lib.send :command, 'pull', [remote, branch]
26
+ end
27
+
28
+ def check_repo_status!
29
+ repo_status_checked = true
30
+ end
31
+
32
+ def path
33
+ dir.to_s
34
+ end
35
+
36
+ # def test_directory
37
+ # File.join(path, '..', ".tmp-git-test-dir-for-#{repo_name}")
38
+ # end
39
+
40
+ def repo_name
41
+ path.split('/').last
42
+ end
43
+
44
+ def clone_to(path)
45
+ Git.clone(self.repo, path)
46
+ end
47
+ alias :clone_to_test :clone_to
48
+ end
49
+ end
@@ -0,0 +1,143 @@
1
+ require 'time'
2
+
3
+ module GitTest
4
+ # orchestrates everything
5
+ class Runner
6
+ attr_accessor :notify, :options, :proj, :test_proj, :test, :writer, :test_dir
7
+
8
+ def initialize(options ={} , notify = Notify.new)
9
+ self.options = options
10
+ self.notify = notify
11
+ self.proj = GitTest::Proj.new(options)
12
+ self.test = GitTest::Test.new(options)
13
+ self.test_dir = Dir.mktmpdir(".git_test_#{proj.current_branch}_#{Time.now.to_i}")
14
+ clone_to_test!
15
+ end
16
+
17
+ # runs the test command provided
18
+ def test!
19
+ in_test_dir do
20
+ proj.check_repo_status!
21
+ notify.start("Running tests on: #{proj.current_branch}")
22
+ test.run!
23
+ notify.done("Test finished: #{test.status}", test.passed?)
24
+ end
25
+ end
26
+
27
+ # will open the specified or last report
28
+ def show_report(file_name = nil, branch = current_branch)
29
+ file_name ||= last_report_file_name(branch)
30
+ report = proj.show(report_branch, File.join(report_path(branch), file_name))
31
+ report_file = Tempfile.new([report_name, report_extension])
32
+ report_file.write(report)
33
+ exec "open #{report_file.path}"
34
+ sleep 300
35
+ end
36
+
37
+ # gives last report file name
38
+ def last_report_file_name(branch = current_branch)
39
+ ls_report_dir(branch).first
40
+ end
41
+
42
+ # outputs the files in the test directory for a given branch
43
+ def ls_report_dir(branch = current_branch)
44
+ files = proj.show(report_branch, report_path(branch)).split("\n")
45
+ files.shift(2)
46
+ files
47
+ end
48
+
49
+
50
+ # cleans up the temp directory
51
+ def clean_test_dir!
52
+ FileUtils.remove_entry_secure test_dir
53
+ end
54
+
55
+ # pushes to origin on the current branch and report branch
56
+ def push!
57
+ notify.write("Pushing to origin")
58
+ proj.push('origin', proj.current_branch)
59
+ proj.push('origin', report_branch) if proj.is_branch? report_branch
60
+ end
61
+
62
+ # pulls from origin on the current branch and report branch
63
+ def pull!
64
+ notify.write("Pulling from origin")
65
+ proj.pull('origin', proj.current_branch)
66
+ end
67
+
68
+ # writes the result of the test command to disk
69
+ def write_report!
70
+ notify.critical_error("Must run `test!` before writing a report") if test.status.nil?
71
+ in_test_dir do
72
+ self.writer = GitTest::Writer.new(:path => report_path,
73
+ :name => report_name,
74
+ :report => test.report )
75
+ in_report_branch do
76
+ writer.save!
77
+ commit_to_test_proj!
78
+ end
79
+ end
80
+ end
81
+
82
+ # commits contents of test branch and pushes back to local repo
83
+ def commit_to_test_proj!
84
+ test_proj.add full_report_path
85
+ result = test_proj.commit("#{proj.current_branch} #{report_name}")
86
+ notify.write("Pushing back to local repo")
87
+ test_proj.real_pull('origin', report_branch)
88
+ test_proj.push('origin', report_branch)
89
+ end
90
+
91
+ private
92
+
93
+ def full_report_path
94
+ File.join(report_path, report_name)
95
+ end
96
+
97
+ def report_branch
98
+ "git_test_reports"
99
+ end
100
+
101
+ def report_path(branch = proj.current_branch)
102
+ "git_test_reports/#{branch}"
103
+ end
104
+
105
+ def report_name
106
+ name = "#{ test.created_at.utc.iso8601 }_"
107
+ name << "#{ proj.username }_"
108
+ name << "#{ test.status }#{ report_extension }"
109
+ end
110
+
111
+ def current_branch
112
+ proj.current_branch
113
+ end
114
+
115
+
116
+
117
+ def report_extension
118
+ ".#{test.format}"
119
+ end
120
+
121
+
122
+ def clone_to_test!
123
+ clone = proj.clone_to_test(test_dir)
124
+ self.test_proj = GitTest::Proj.new(:path => clone.dir.to_s)
125
+ end
126
+
127
+ def in_test_dir(&block)
128
+ Dir.chdir(test_dir) do
129
+ yield
130
+ end
131
+ end
132
+
133
+ def in_report_branch(branch = report_branch, &block)
134
+ in_test_dir do
135
+ branch = test_proj.branch(branch)
136
+ branch.checkout
137
+ test_proj.checkout(branch)
138
+ yield
139
+ test_proj.checkout(proj.current_branch)
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,28 @@
1
+ module GitTest
2
+ class Test
3
+ attr_accessor :report, :status, :notify, :created_at, :command, :exit_code
4
+ def initialize(options = {}, notify = Notify.new)
5
+ self.notify = notify
6
+ self.created_at = Time.now
7
+ self.command = options[:cmd]
8
+ end
9
+
10
+ def format
11
+ :html
12
+ end
13
+
14
+ def passed?
15
+ exit_code == 0
16
+ end
17
+
18
+ def failed?
19
+ !passed?
20
+ end
21
+
22
+ def run!
23
+ self.report = %x{#{command}}
24
+ self.exit_code = Integer($?)
25
+ self.status = passed? ? "passed" : "failed"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ require 'fileutils.rb'
2
+
3
+
4
+ module GitTest
5
+ # takes a path, a name (file name) and a report
6
+ # makes directory if not present
7
+ # writes the given report to the file name in that path
8
+ class Writer
9
+ attr_accessor :report, :path, :name, :notify
10
+
11
+ def initialize(options, notify = Notify.new)
12
+ self.path = options[:path]
13
+ self.name = options[:name]
14
+ self.report = options[:report]
15
+ self.notify = notify
16
+ end
17
+
18
+
19
+ def mkdir!
20
+ FileUtils.mkdir_p(path)
21
+ end
22
+
23
+ def full_path
24
+ File.join(path, name)
25
+ end
26
+
27
+ def write_report_to_disk!
28
+ mkdir!
29
+ File.open(full_path, 'w') {|f| f.write(report) }
30
+ end
31
+
32
+ def self.save(*args)
33
+ self.new(*args).save
34
+ end
35
+
36
+ def save
37
+ notify.start("Writing report to #{full_path}")
38
+ write_report_to_disk!
39
+ end
40
+ alias :save! :save
41
+
42
+ def show_report
43
+ system "open #{}"
44
+ end
45
+ end
46
+ end
data/lib/git_test.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'git'
3
+ require 'colorize'
4
+ require 'growl'
5
+
6
+ module GitTest
7
+
8
+ end
9
+
10
+ require 'lib/git_test/notify.rb'
11
+ require 'lib/git_test/runner'
12
+ require 'lib/git_test/test'
13
+ require 'lib/git_test/writer'
14
+ require 'lib/git_test/proj'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git_test
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Richard Schneeman
@@ -181,6 +181,12 @@ extra_rdoc_files:
181
181
  - README.md
182
182
  files:
183
183
  - bin/git_test
184
+ - lib/git_test.rb
185
+ - lib/git_test/notify.rb
186
+ - lib/git_test/proj.rb
187
+ - lib/git_test/runner.rb
188
+ - lib/git_test/test.rb
189
+ - lib/git_test/writer.rb
184
190
  - README.md
185
191
  homepage: https://github.com/schneems/git_test
186
192
  licenses: []