fit-commit 3.2.2 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a192b55fd47925dbae22610a7fc15811751d0b57
4
- data.tar.gz: 58e5247361be7806cbf568b05ef5f10bb8a9a811
3
+ metadata.gz: 8c1962d7ad212ab2d64654448e4ec61fa069636f
4
+ data.tar.gz: c8942246016315fa610507e5e07ecb978caa4563
5
5
  SHA512:
6
- metadata.gz: 3d09f8222037da2a6f794870762e7720e49f86afb45fdd233a16910ba2c14d1b73deae27179ff01cb7ec67a09d41691302d48ab38e3ee060a411982e7378b463
7
- data.tar.gz: c4ecf05c835b1d2d4f0896e82c535f05737b386985520dfa4341cf0f98cf8ed455299ef983977b7971a6243a64097b0ebb19cce5d4bde0eb5a318e23c3c6b2fd
6
+ metadata.gz: 5fa212d38361178174a815e1c26f899d4691c71d7bbb01b42a01397bda93db1a574b66ef591bc01d61bf7e6954834d39349e52cd111c8c6a7905c42c1e55d4bb
7
+ data.tar.gz: 503e76de5b61998831d17a5d47c9da2166597c390f120fe8963b12fe94a9e840f5db25b891380a363bbc3f324ad2309bbcc7a59065c0b11579984938968dea1f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ### v3.3.0 (2015-10-10)
4
+ - Add support for custom validators
5
+
3
6
  ### v3.2.0 (2015-10-03)
4
7
  - Add option to re-open text editor
5
8
 
data/README.md CHANGED
@@ -82,6 +82,46 @@ Validators/Bar:
82
82
  - !ruby/regexp /\Afoo.+bar/
83
83
  ```
84
84
 
85
+ ## Adding custom validators
86
+
87
+ Create your custom validator as a `FitCommit::Validators::Base` subclass:
88
+
89
+ ```ruby
90
+ module FitCommit
91
+ module Validators
92
+ class MyCustomValidator < Base
93
+ def validate_line(lineno, text)
94
+ if text =~ /sneak peak/i
95
+ add_error(lineno, "I think you mean 'sneak peek'.")
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ ```
102
+
103
+ `Require` the file and enable the validator in your config:
104
+
105
+ ```yaml
106
+ FitCommit:
107
+ Require:
108
+ - somedir/my_custom_validator.rb
109
+ Validators/MyCustomValidator:
110
+ Enabled: true
111
+ ```
112
+
113
+ You can also publish your validator as a gem, and require it that way:
114
+
115
+ ```yaml
116
+ FitCommit:
117
+ Require:
118
+ - my-custom-validator-gem
119
+ Validators/MyCustomValidator:
120
+ Enabled: true
121
+ ```
122
+
123
+ If others might find your validator useful, submit it as a Pull Request. If it's not useful for everyone, it can be disabled by default.
124
+
85
125
  ## FAQ
86
126
 
87
127
  ### Can Fit Commit run in all my repos without having to install it each time?
@@ -45,7 +45,7 @@ module FitCommit
45
45
 
46
46
  def read_config(path)
47
47
  load_yaml(path).each_with_object({}) do |(key, value), config|
48
- translated_key = translate_class_name(key)
48
+ translated_key = translate_config_key(key)
49
49
  config[translated_key] = value
50
50
  end
51
51
  end
@@ -57,8 +57,9 @@ module FitCommit
57
57
  raise e, "Error parsing config file: #{e.message}"
58
58
  end
59
59
 
60
- def translate_class_name(config_class_name)
61
- "FitCommit::" + config_class_name.gsub("/", "::")
60
+ def translate_config_key(config_key)
61
+ return config_key unless config_key.include?("/")
62
+ "FitCommit::" + config_key.gsub("/", "::")
62
63
  end
63
64
  end
64
65
  end
@@ -19,12 +19,25 @@ module FitCommit
19
19
  end
20
20
 
21
21
  def all_validators
22
- Dir[File.dirname(__FILE__) + "/validators/*.rb"].each { |file| require file }
22
+ require_all_validators
23
23
  FitCommit::Validators::Base.all.map do |validator_class|
24
24
  validator_class.new(branch_name, config_for(validator_class))
25
25
  end
26
26
  end
27
27
 
28
+ def require_all_validators
29
+ paths = Dir[File.dirname(__FILE__) + "/validators/*.rb"] + custom_requires
30
+ paths.each { |file| require file }
31
+ end
32
+
33
+ def custom_requires
34
+ Array(global_settings["Require"])
35
+ end
36
+
37
+ def global_settings
38
+ configuration["FitCommit"] || {}
39
+ end
40
+
28
41
  def config_for(validator_class)
29
42
  configuration[validator_class.name] || {}
30
43
  end
@@ -1,3 +1,3 @@
1
1
  module FitCommit
2
- VERSION = "3.2.2"
2
+ VERSION = "3.3.0"
3
3
  end
@@ -16,3 +16,5 @@ Validators/Wip:
16
16
  Validators/Frathouse:
17
17
  Enabled:
18
18
  - master
19
+ Validators/Foobar:
20
+ Enabled: true
@@ -1,4 +1,4 @@
1
- require "minitest/autorun"
1
+ require "test_helper"
2
2
  require "pty"
3
3
  require "expect"
4
4
 
@@ -0,0 +1,8 @@
1
+ require "minitest/autorun"
2
+
3
+ def create_tempfile(filename, content)
4
+ Tempfile.new(filename).tap do |f|
5
+ f.write(content)
6
+ f.close
7
+ end
8
+ end
@@ -1,4 +1,4 @@
1
- require "minitest/autorun"
1
+ require "test_helper"
2
2
  require "fit_commit/configuration_loader"
3
3
 
4
4
  describe FitCommit::ConfigurationLoader do
@@ -24,13 +24,6 @@ describe FitCommit::ConfigurationLoader do
24
24
  end
25
25
  end
26
26
 
27
- def tempfile(filename, content)
28
- Tempfile.new(filename).tap do |f|
29
- f.write(content)
30
- f.close
31
- end
32
- end
33
-
34
27
  describe "no configuration files present" do
35
28
  let(:system_file) { nil }
36
29
  let(:user_file) { nil }
@@ -41,7 +34,7 @@ describe FitCommit::ConfigurationLoader do
41
34
 
42
35
  describe "just one configuration file present" do
43
36
  let(:system_file) { nil }
44
- let(:user_file) { tempfile("user_file", user_file_content) }
37
+ let(:user_file) { create_tempfile("user_file", user_file_content) }
45
38
  let(:user_file_content) do
46
39
  "Foo/Bar:\n Baz: false\nQux/Norf/Blah:\n - !ruby/regexp /\\Afoo/"
47
40
  end
@@ -53,23 +46,35 @@ describe FitCommit::ConfigurationLoader do
53
46
  }
54
47
  assert_equal expected, global_configuration
55
48
  end
49
+
50
+ describe "has a non-validation key" do
51
+ let(:user_file_content) do
52
+ "FitCommit:\n Require:\n - foo/bar"
53
+ end
54
+ it "doesn't try to namespace the key" do
55
+ expected = {
56
+ "FitCommit" => { "Require" => ["foo/bar"] }
57
+ }
58
+ assert_equal expected, global_configuration
59
+ end
60
+ end
56
61
  end
57
62
 
58
63
  describe "multiple configuration files present" do
59
- let(:system_file) { tempfile("system_file", system_file_content) }
60
- let(:user_file) { tempfile("user_file", user_file_content) }
64
+ let(:system_file) { create_tempfile("system_file", system_file_content) }
65
+ let(:user_file) { create_tempfile("user_file", user_file_content) }
61
66
  let(:system_file_content) do
62
67
  "Foo/Bar:\n Baz: false\nQux/Norf/Blah:\n Foobar:\n - !ruby/regexp /\\Afoo/\n Booyah: false"
63
68
  end
64
69
  let(:user_file_content) do
65
- "Qux/Norf/Blah:\n Foobar: true\nBuz:\n - hi"
70
+ "Qux/Norf/Blah:\n Foobar: true\nAbc/Buz:\n - hi"
66
71
  end
67
72
 
68
73
  it "is a merged configuration that takes precedence into account" do
69
74
  expected = {
70
75
  "FitCommit::Foo::Bar" => { "Baz" => false },
71
76
  "FitCommit::Qux::Norf::Blah" => { "Foobar" => true, "Booyah" => false },
72
- "FitCommit::Buz" => ["hi"]
77
+ "FitCommit::Abc::Buz" => ["hi"]
73
78
  }
74
79
  assert_equal expected, global_configuration
75
80
  end
@@ -1,4 +1,4 @@
1
- require "minitest/autorun"
1
+ require "test_helper"
2
2
  require "fit_commit/message_parser"
3
3
 
4
4
  describe FitCommit::MessageParser do
@@ -6,16 +6,8 @@ describe FitCommit::MessageParser do
6
6
  commit_msg_file.unlink
7
7
  end
8
8
 
9
- let(:commit_msg_file) do
10
- Tempfile.new("test-commit-msg").tap do |f|
11
- f.write(commit_msg)
12
- f.close
13
- end
14
- end
15
-
16
- let(:lines) do
17
- FitCommit::MessageParser.new(commit_msg_file.path).lines
18
- end
9
+ let(:commit_msg_file) { create_tempfile("test-commit-msg", commit_msg) }
10
+ let(:lines) { FitCommit::MessageParser.new(commit_msg_file.path).lines }
19
11
 
20
12
  describe "empty commit msg" do
21
13
  let(:commit_msg) { "" }
@@ -1,23 +1,11 @@
1
- require "minitest/autorun"
1
+ require "test_helper"
2
2
  require "fit_commit/runner"
3
3
 
4
4
  describe FitCommit::Runner do
5
5
  after do
6
6
  commit_msg_file.unlink
7
7
  end
8
-
9
- def call_runner
10
- exit_code = runner.run
11
- stderr.rewind
12
- exit_code
13
- end
14
-
15
- let(:commit_msg_file) do
16
- Tempfile.new("test-commit-msg").tap do |f|
17
- f.write(commit_msg)
18
- f.close
19
- end
20
- end
8
+ let(:commit_msg_file) { create_tempfile("test-commit-msg", commit_msg) }
21
9
  let(:stderr) { StringIO.new }
22
10
  let(:stdin) { StringIO.new }
23
11
  let(:branch_name) { "any" }
@@ -25,6 +13,12 @@ describe FitCommit::Runner do
25
13
  FitCommit::Runner.new(commit_msg_file.path, branch_name, stderr, stdin)
26
14
  end
27
15
 
16
+ def call_runner
17
+ exit_code = runner.run
18
+ stderr.rewind
19
+ exit_code
20
+ end
21
+
28
22
  describe "empty commit msg" do
29
23
  let(:commit_msg) { "" }
30
24
  it "allows commit without printing to stderr" do
@@ -1,4 +1,4 @@
1
- require "minitest/autorun"
1
+ require "test_helper"
2
2
  require "fit_commit/validator_loader"
3
3
  Dir[File.dirname(__FILE__) + "/validators/*.rb"].each { |file| require file }
4
4
 
@@ -64,4 +64,47 @@ describe FitCommit::ValidatorLoader do
64
64
  assert validators.none? { |v| v.is_a? FitCommit::Validators::Frathouse }
65
65
  end
66
66
  end
67
+
68
+ describe "custom validators required" do
69
+ after do
70
+ custom_validator_file.unlink
71
+ end
72
+
73
+ let(:custom_validator_file) do
74
+ create_tempfile(["my_custom_validator", ".rb"], custom_validator_code)
75
+ end
76
+
77
+ # Not actually subclassing FitCommit::Validators::Base because
78
+ # that affects the loaded validators for other tests.
79
+ let(:custom_validator_code) do
80
+ <<-EOF
81
+ class MyCustomValidator
82
+ attr_accessor :config
83
+ def initialize(_branch_name, config)
84
+ self.config = config
85
+ end
86
+ def enabled?
87
+ config.fetch("Enabled")
88
+ end
89
+ end
90
+ EOF
91
+ end
92
+
93
+ let(:configuration) do
94
+ all_disabled_configuration.merge(
95
+ "FitCommit" => { "Require" => [custom_validator_file.path] },
96
+ "MyCustomValidator" => { "Enabled" => true }
97
+ )
98
+ end
99
+
100
+ it "loads enabled custom validator" do
101
+ # stub which will be overridden when the custom validator file is loaded
102
+ MyCustomValidator = Class.new
103
+
104
+ FitCommit::Validators::Base.stub(:all, [MyCustomValidator]) do
105
+ assert_equal 1, validators.size
106
+ assert validators.first.is_a?(MyCustomValidator)
107
+ end
108
+ end
109
+ end
67
110
  end
@@ -1,4 +1,4 @@
1
- require "minitest/autorun"
1
+ require "test_helper"
2
2
  require "yaml"
3
3
 
4
4
  def default_config_for(key)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fit-commit
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Foley
@@ -91,6 +91,7 @@ files:
91
91
  - templates/config/fit_commit.default.yml
92
92
  - templates/hooks/commit-msg
93
93
  - test/integration/new_repo_test.rb
94
+ - test/test_helper.rb
94
95
  - test/unit/configuration_loader_test.rb
95
96
  - test/unit/message_parser_test.rb
96
97
  - test/unit/runner_test.rb
@@ -136,6 +137,7 @@ specification_version: 4
136
137
  summary: A Git hook to validate your commit messages
137
138
  test_files:
138
139
  - test/integration/new_repo_test.rb
140
+ - test/test_helper.rb
139
141
  - test/unit/configuration_loader_test.rb
140
142
  - test/unit/message_parser_test.rb
141
143
  - test/unit/runner_test.rb