fit-commit 3.1.0 → 3.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8699deb0617713906feec2843b053835d4eff93a
4
- data.tar.gz: f7131e71ae0921ce4aaf1f6873331b008d769d1f
3
+ metadata.gz: bb59c8dd8e5d52763773a9a53ccc33a1b4986ed7
4
+ data.tar.gz: 246cb45ef47a4f77e4dc24ef18c3adc824075bf9
5
5
  SHA512:
6
- metadata.gz: f4878977c4b1aa0aadc837053ffb3afddb63c9aeb46d6bd6bf24f78c86dab3da74cf61c41ca4e3cb3de78b8bd3d2737e19cec5e75406e5edf9c234eb2ee36c40
7
- data.tar.gz: cac7ba464d93602fb88462dc96de993feb35ca9c5b6b0d10c69f7e4fbede134d2d35b50c81987155125d4bd1a29513687351338080d83e6a87bb258f67791cbf
6
+ metadata.gz: 5cd0c0d67d1eebfbe38ace9b7d7f769af567d6becd2c981489413fa2e66ceb12b4338abe5379951d67a120575af0bb86a42762176d14cebb655c5f20e9c7a64d
7
+ data.tar.gz: 7a7c003885b3dff14836482a71b065be8cfb6c36687cb9327204898132847b1ca19ab9a58f9505f07338ac0122fc41d620af7876adaab0e797cced33c68746d9
@@ -43,3 +43,12 @@ Style/FileName:
43
43
 
44
44
  Style/DotPosition:
45
45
  EnforcedStyle: trailing
46
+
47
+ Style/IndentationWidth:
48
+ Enabled: false
49
+
50
+ Style/ElseAlignment:
51
+ Enabled: false
52
+
53
+ Style/RegexpLiteral:
54
+ Enabled: false
data/README.md CHANGED
@@ -14,7 +14,7 @@ foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar
14
14
  2: Error: Second line must be blank.
15
15
  3: Error: Lines should be <= 72 chars. (76)
16
16
 
17
- Force commit? [y/n] ▊
17
+ Force commit? [y/n/e] ▊
18
18
  ```
19
19
 
20
20
  ## Prerequisites
@@ -25,6 +25,14 @@ module FitCommit
25
25
  merge_hashes(warnings, other_warnings)
26
26
  end
27
27
 
28
+ def clear_errors
29
+ @errors = Hash.new([])
30
+ end
31
+
32
+ def clear_warnings
33
+ @warnings = Hash.new([])
34
+ end
35
+
28
36
  private
29
37
 
30
38
  def merge_hashes(error_hash, other_hash)
@@ -18,11 +18,13 @@ module FitCommit
18
18
  end
19
19
 
20
20
  def run
21
- return EXIT_CODE_ALLOW_COMMIT if empty_commit?
22
- run_validators
23
- return EXIT_CODE_ALLOW_COMMIT if [errors, warnings].all?(&:empty?)
24
- print_results
25
- allow_commit = errors.empty? || ask_force_commit
21
+ allow_commit = retry_on_user_edit do
22
+ return EXIT_CODE_ALLOW_COMMIT if empty_commit?
23
+ run_validators
24
+ return EXIT_CODE_ALLOW_COMMIT if [errors, warnings].all?(&:empty?)
25
+ print_results
26
+ errors.empty? || ask_force_commit
27
+ end
26
28
 
27
29
  if allow_commit
28
30
  stderr.print "\n"
@@ -36,10 +38,23 @@ module FitCommit
36
38
 
37
39
  private
38
40
 
41
+ StartOverOnEditException = Class.new(StandardError)
42
+
43
+ def retry_on_user_edit
44
+ yield
45
+ rescue StartOverOnEditException
46
+ clear_lines
47
+ clear_errors
48
+ clear_warnings
49
+ edit_message && retry
50
+ end
51
+
39
52
  def ask_force_commit
40
53
  return unless interactive?
41
- stderr.print "\nForce commit? [y/n] "
42
- stdin.gets =~ /y/i
54
+ stderr.print "\nForce commit? [y/n/e] "
55
+ input = stdin.gets
56
+ fail StartOverOnEditException if input =~ /e/i
57
+ input =~ /y/i
43
58
  end
44
59
 
45
60
  def interactive?
@@ -78,8 +93,24 @@ module FitCommit
78
93
  @lines ||= FitCommit::MessageParser.new(message_path).lines
79
94
  end
80
95
 
96
+ def clear_lines
97
+ @lines = nil
98
+ end
99
+
81
100
  def empty_commit?
82
101
  lines.all?(&:empty?)
83
102
  end
103
+
104
+ def edit_message
105
+ system(editor, message_path)
106
+ end
107
+
108
+ DEFAULT_EDITOR = "vim"
109
+
110
+ def editor
111
+ editor = ENV["EDITOR"]
112
+ editor = DEFAULT_EDITOR unless editor && editor != "none"
113
+ editor
114
+ end
84
115
  end
85
116
  end
@@ -1,3 +1,3 @@
1
1
  module FitCommit
2
- VERSION = "3.1.0"
2
+ VERSION = "3.2.0"
3
3
  end
@@ -65,10 +65,17 @@ describe FitCommit::Runner do
65
65
  describe "commit msg contains errors" do
66
66
  let(:commit_msg) { "foo.\nbar" }
67
67
 
68
- def assert_error_output(interactive: true)
68
+ def assert_error_output(interactive: true, includes_edit: false)
69
69
  stderr_lines = stderr.read.lines.map(&:chomp)
70
- expected_lines = interactive ? 8 : 6
71
- assert_equal expected_lines, stderr_lines.size
70
+ if interactive
71
+ if includes_edit
72
+ assert_equal 13, stderr_lines.size
73
+ else
74
+ assert_equal 8, stderr_lines.size
75
+ end
76
+ else
77
+ assert_equal 6, stderr_lines.size
78
+ end
72
79
  assert_equal commit_msg, stderr_lines[0..1].join("\n")
73
80
  assert_empty stderr_lines[2]
74
81
  assert_match(/\A1: Error: /, stderr_lines[3])
@@ -76,7 +83,7 @@ describe FitCommit::Runner do
76
83
  assert_match(/\A2: Error: /, stderr_lines[5])
77
84
  if interactive
78
85
  assert_empty stderr_lines[6]
79
- assert_equal "Force commit? [y/n] ", stderr_lines[7]
86
+ assert_match(/\AForce commit\? \[y\/n\/e\] /, stderr_lines[7])
80
87
  end
81
88
  end
82
89
 
@@ -102,6 +109,56 @@ describe FitCommit::Runner do
102
109
  assert_error_output
103
110
  end
104
111
  end
112
+ describe "user edits commit" do
113
+ let(:stdin) { fake_tty("e") }
114
+ describe "editor fails to save" do
115
+ it "rejects commit" do
116
+ def runner.edit_message
117
+ nil # fail code
118
+ end
119
+ assert_equal FitCommit::Runner::EXIT_CODE_REJECT_COMMIT, call_runner
120
+ assert_error_output
121
+ end
122
+ end
123
+ describe "edited message is valid" do
124
+ before do
125
+ def runner.edit_message
126
+ File.open(message_path, "w") do |f|
127
+ f.print "Valid message"
128
+ end
129
+ :success_code
130
+ end
131
+ end
132
+ it "allows commit" do
133
+ assert_equal FitCommit::Runner::EXIT_CODE_ALLOW_COMMIT, call_runner
134
+ assert_error_output
135
+ end
136
+ end
137
+ describe "edited message is invalid" do
138
+ before do
139
+ def runner.edit_message
140
+ File.open(message_path, "w") do |f|
141
+ f.print "invalid message."
142
+ end
143
+ :success_code
144
+ end
145
+ end
146
+ describe "user forces second commit" do
147
+ let(:stdin) { fake_tty("e\ny") }
148
+ it "asks again and accepts commit" do
149
+ assert_equal FitCommit::Runner::EXIT_CODE_ALLOW_COMMIT, call_runner
150
+ assert_error_output(includes_edit: true)
151
+ end
152
+ end
153
+ describe "user doesn't force second commit" do
154
+ let(:stdin) { fake_tty("e\nn") }
155
+ it "asks again and rejects commit" do
156
+ assert_equal FitCommit::Runner::EXIT_CODE_REJECT_COMMIT, call_runner
157
+ assert_error_output(includes_edit: true)
158
+ end
159
+ end
160
+ end
161
+ end
105
162
  describe "TTY not available" do
106
163
  let(:stdin) { StringIO.new("") }
107
164
  it "prints errors to stderr and rejects commit" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fit-commit
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Foley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-28 00:00:00.000000000 Z
11
+ date: 2015-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: swearjar