danger-logging_lint 0.0.3 → 0.0.4

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
  SHA256:
3
- metadata.gz: 9502be7bec98f9ac55351ec4b27e2ecac0140be3974d2be654400338633d3223
4
- data.tar.gz: ecdde9f1dcc8c1807733f41aedbbb92e7465ddbc7dfb62b7375f11e52ea3e9a8
3
+ metadata.gz: a08e90afc0c447a3f28d9afe54ee806eff0c0f4b4baa203d006c850ee9f59f51
4
+ data.tar.gz: e4b1797375798f5e2c00bf6c51feec0292b343887d2f16719d238dc9bc0ded2c
5
5
  SHA512:
6
- metadata.gz: 476adf208a82fb33e9b65d48749d7c9b0f267b5dbc46c231cb42027bae840285d264b3056a19740c07fd4e9c06c47ab6c2bd73bb9af2f88bb873fe2ebd27c907
7
- data.tar.gz: 4583a38b9c4d22b94cdec222e545714519db132d52a8e526f270b9dc2fe7765bd947623c6a48cf926f8d60cb0d257039b2422cc90cf8efd46fd61532fd44b13f
6
+ metadata.gz: 685a9d6243a365cc2098b5f9ad36aeb097d55ed06e08b0a546675f9e9393675e1f01e097c89bfdeebc66dfe11fad1570fe1847eea338fdd927dad385b0202720
7
+ data.tar.gz: e2b4e4e4883faf5436c814cc5d0b2672ef9d8db7b1202737279aeda490f8f79195882fb839847d1fed93ccae7cf15e32d1f686c999b838842acc261cc6b9f77f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## Changelog
2
2
 
3
+ ### Version 0.0.4 (2022-04-28)
4
+
5
+ - Fixed crash when changed file is a directory (filters them out).
6
+ - Fixed crash when opening missing file (filters them out).
7
+ - Split rspec into multiple files.
8
+ - Added tests for linter with all variables set using Danger file.
9
+ - Variables used in multiple tests are defined as constants in `spec_helper.rb`.
10
+
3
11
  ### Version 0.0.3 (2022-04-22)
4
12
 
5
13
  - Added deploy and test yaml for github workflow.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danger-logging_lint (0.0.3)
4
+ danger-logging_lint (0.0.4)
5
5
  danger-plugin-api (~> 1.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  ## Logging Lint
2
2
  [![Gem Version](https://badge.fury.io/rb/danger-logging_lint.svg)](https://badge.fury.io/rb/danger-logging_lint) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/eManPrague/danger-logging_lint/blob/master/LICENSE.txt) [![Test](https://github.com/eManPrague/danger-logging_lint/actions/workflows/test.yml/badge.svg)](https://github.com/eManPrague/danger-logging_lint/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/eManPrague/danger-logging_lint/branch/master/graph/badge.svg?token=Z2RZKYNBVI)](https://codecov.io/gh/eManPrague/danger-logging_lint)
3
3
 
4
-
5
4
  This danger plugin can be used to check log lines in modified (added) files. It heavily relies on regex configuration which can be modified to search all kinds of parts of code in the files. Default configuration is set to support [Kotlin eMan Logger Library](https://github.com/eManPrague/logger-ktx). Ex: logInfo { "Info message $var" }.
6
5
 
7
6
  It works in two steps. First it searches for all log lines (multilines) in files. And then it applies line variable regex combined with line remove regex. Check `check_files` function for more information.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LoggingLint
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
@@ -191,28 +191,41 @@ module Danger
191
191
  #
192
192
  def log_lint
193
193
  if log_functions.nil? || log_functions.size <= 0
194
- self.fail("No log functions are defined. Please check your Danger file.")
194
+ self.fail("Logging lint: No log functions are defined. Please check your Danger file.")
195
195
  return
196
196
  end
197
197
 
198
198
  if line_variable_regex.nil? || line_variable_regex.size <= 0
199
- message("At least one variable index must be defined (using default). Please check your Danger file.")
199
+ message("Logging lint: At least one variable index must be defined (using default). Please check your Danger file.")
200
200
  end
201
201
 
202
202
  target_files = (git.modified_files - git.deleted_files) + git.added_files
203
+ target_files = target_files.reject { |filename| invalid_file?(filename) }
203
204
  if !file_extensions.nil? && file_extensions.size >= 0
204
205
  file_extensions_regex = "(.#{file_extensions.join('|.')})"
205
206
  target_files = target_files.grep(/#{file_extensions_regex}/)
206
207
  end
207
208
 
208
209
  if target_files.empty?
209
- message("No files to check.")
210
+ message("Logging lint: No files to check.")
210
211
  return
211
212
  end
212
213
 
213
214
  check_files(target_files)
214
215
  end
215
216
 
217
+ #
218
+ # Checks if file is not valid. It will not be valid in two cases:
219
+ # 1) Files is a directory.
220
+ # 2) Files does not exist.
221
+ # In both cases we cannot open it and lint it. There is also no reason to lint them.
222
+ #
223
+ # @return [Boolean] true if invalid file
224
+ #
225
+ def invalid_file?(filename)
226
+ File.directory?(filename) || !File.exist?(filename)
227
+ end
228
+
216
229
  #
217
230
  # Checks all files for log violations based on log regex and log function. Each log function id extended by log
218
231
  # regex and searched for (format: #log_function#log_regex). Each of such found line is then checked if it contains a
@@ -224,6 +237,8 @@ module Danger
224
237
  def check_files(files)
225
238
  raw_file = ""
226
239
  files.each do |filename|
240
+ next if invalid_file?(filename)
241
+
227
242
  raw_file = File.read(filename)
228
243
  log_functions.each do |log_function|
229
244
  raw_file.scan(/#{log_function}#{log_regex}/m) do |c|
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("spec_helper", __dir__)
4
+
5
+ #
6
+ # Tests for situations when the linter does not run because of either configuration or there are no files to check.
7
+ #
8
+
9
+ module Danger
10
+ describe Danger::DangerLoggingLint do
11
+ #
12
+ # Defines linter, danger file and other variables used by the linter.
13
+ #
14
+ describe "with Dangerfile" do
15
+ before do
16
+ @dangerfile = testing_dangerfile
17
+ @logging_lint = @dangerfile.logging_lint
18
+
19
+ mock_variables(@logging_lint)
20
+ end
21
+
22
+ #
23
+ # Test for logging lines in cases when linter does not run (either by config or file settings).
24
+ #
25
+
26
+ it "Error is printed when log functions are not configured" do
27
+ allow(@logging_lint).to receive(:log_functions).and_return([])
28
+ @logging_lint.log_lint
29
+ expect(@dangerfile.status_report[:errors]).to eq(["Logging lint: No log functions are defined. Please check your Danger file."])
30
+ end
31
+
32
+ it "Error is printed when log variable regex is not configured" do
33
+ allow(@logging_lint).to receive(:line_variable_regex).and_return([])
34
+ @logging_lint.log_lint
35
+ expect(@dangerfile.status_report[:messages][0]).to eq("Logging lint: At least one variable index must be defined (using default). Please check your Danger file.")
36
+ end
37
+
38
+ it "Nothing is printed when there are no files to check" do
39
+ @logging_lint.log_lint
40
+ expect(@dangerfile.status_report[:errors]).to eq([])
41
+ expect(@dangerfile.status_report[:messages][0]).to eq("Logging lint: No files to check.")
42
+ end
43
+
44
+ it "Nothing is printed when there are only folders to check" do
45
+ allow(@logging_lint).to receive(:file_extensions).and_return([])
46
+ allow(@logging_lint.git).to receive(:modified_files).and_return(%W(#{DIR_NAME}))
47
+ @logging_lint.log_lint
48
+ expect(@dangerfile.status_report[:errors]).to eq([])
49
+ end
50
+
51
+ it "Nothing is printed when there are no files to check (filtered by extensions)" do
52
+ allow(@logging_lint.git).to receive(:modified_files).and_return(MODIFIED_FILES)
53
+ allow(@logging_lint).to receive(:file_extensions).and_return(%w(unknownExtension))
54
+ @logging_lint.log_lint
55
+ expect(@dangerfile.status_report[:errors]).to eq([])
56
+ end
57
+ end
58
+ end
59
+ end
@@ -8,11 +8,6 @@ module Danger
8
8
  expect(Danger::DangerLoggingLint.new(nil)).to be_a Danger::Plugin
9
9
  end
10
10
 
11
- dir_name = File.dirname(__FILE__)
12
- modified_files = %W(#{dir_name}/fixtures/ModifiedFile.kt #{dir_name}/fixtures/IgnoredModifiedFile.txt)
13
- added_files = %W(#{dir_name}/fixtures/NewFile.kt)
14
- warning_text = "Does this log comply with security rules?"
15
-
16
11
  #
17
12
  # Defines linter, danger file and other variables used by the linter.
18
13
  #
@@ -25,48 +20,27 @@ module Danger
25
20
  allow(@logging_lint.git).to receive(:added_files).and_return([])
26
21
  allow(@logging_lint.git).to receive(:modified_files).and_return([])
27
22
  allow(@logging_lint).to receive(:file_extensions).and_return(%w(kt))
23
+ allow(@logging_lint).to receive(:log_functions).and_call_original
24
+ allow(@logging_lint).to receive(:warning_text).and_call_original
25
+ allow(@logging_lint).to receive(:log_regex).and_call_original
26
+ allow(@logging_lint).to receive(:line_variable_regex).and_call_original
27
+ allow(@logging_lint).to receive(:line_remove_regex).and_call_original
28
28
  end
29
29
 
30
30
  #
31
- # Test for logging lines in cases when linter does not run (either by config or file settings).
31
+ # Test for logging lines in cases when linter does run.
32
32
  #
33
33
 
34
- it "Error is printed when log functions are not configured" do
35
- allow(@logging_lint).to receive(:log_functions).and_return([])
36
- @logging_lint.log_lint
37
- expect(@dangerfile.status_report[:errors]).to eq(["No log functions are defined. Please check your Danger file."])
38
- end
39
-
40
- it "Error is printed when log variable regex is not configured" do
41
- allow(@logging_lint).to receive(:line_variable_regex).and_return([])
42
- @logging_lint.log_lint
43
- expect(@dangerfile.status_report[:messages][0]).to eq("At least one variable index must be defined (using default). Please check your Danger file.")
44
- end
45
-
46
- it "Nothing is printed when there are no files to check" do
47
- @logging_lint.log_lint
48
- expect(@dangerfile.status_report[:errors]).to eq([])
49
- end
50
-
51
- it "Nothing is printed when there are no files to check (filtered by extensions)" do
52
- allow(@logging_lint.git).to receive(:modified_files).and_return(modified_files)
53
- allow(@logging_lint).to receive(:file_extensions).and_return(%w(unknownExtension))
54
- @logging_lint.log_lint
55
- expect(@dangerfile.status_report[:errors]).to eq([])
56
- end
57
-
58
- it "Nothing is printed when log levels are not present" do
59
- allow(@logging_lint).to receive(:log_functions).and_return(%w(missingLogLevel))
34
+ it "Nothing is printed when log functions are not present in files" do
35
+ allow(@logging_lint.git).to receive(:modified_files).and_return(MODIFIED_FILES)
36
+ allow(@logging_lint.git).to receive(:added_files).and_return(ADDED_FILES)
37
+ allow(@logging_lint).to receive(:log_functions).and_return(%w(unknownLogLevel))
60
38
  @logging_lint.log_lint
61
39
  expect(@dangerfile.status_report[:warnings]).to eq([])
62
40
  end
63
41
 
64
- #
65
- # Test for logging lines in cases when linter does run.
66
- #
67
-
68
42
  it "Log with variables is warned for modified files (end line index)" do
69
- allow(@logging_lint.git).to receive(:modified_files).and_return(modified_files)
43
+ allow(@logging_lint.git).to receive(:modified_files).and_return(MODIFIED_FILES)
70
44
  allow(@logging_lint).to receive(:line_index_position).and_return("end")
71
45
  @logging_lint.log_lint
72
46
  violation_lines = [63, 64, 73, 76, 88, 92, 97, 98, 101, 106, 107, 110]
@@ -74,7 +48,7 @@ module Danger
74
48
  end
75
49
 
76
50
  it "Log with variables is warned for modified files (start line index)" do
77
- allow(@logging_lint.git).to receive(:modified_files).and_return(modified_files)
51
+ allow(@logging_lint.git).to receive(:modified_files).and_return(MODIFIED_FILES)
78
52
  allow(@logging_lint).to receive(:line_index_position).and_return("start")
79
53
  @logging_lint.log_lint
80
54
  violation_lines = [63, 64, 71, 74, 85, 89, 93, 98, 99, 102, 107, 108]
@@ -82,36 +56,39 @@ module Danger
82
56
  end
83
57
 
84
58
  it "Log with variables is warned for modified files (middle line index)" do
85
- allow(@logging_lint.git).to receive(:modified_files).and_return(modified_files)
59
+ allow(@logging_lint.git).to receive(:modified_files).and_return(MODIFIED_FILES)
86
60
  allow(@logging_lint).to receive(:line_index_position).and_return("middle")
87
61
  @logging_lint.log_lint
88
62
  violation_lines = [63, 64, 73, 76, 88, 92, 97, 98, 101, 106, 107, 110]
89
63
  compare_warning_with_lines(violation_lines)
90
64
  end
91
65
 
92
- it "Log with variables is warned for new files" do
93
- allow(@logging_lint.git).to receive(:added_files).and_return(added_files)
66
+ it "Log with variables is warned for modified files without crashing due to missing files" do
67
+ allow(@logging_lint.git).to receive(:modified_files).and_return(MODIFIED_FILES + %W(#{DIR_NAME}/fixtures/MissingFile.kt))
94
68
  @logging_lint.log_lint
95
- violation_lines = [47, 48, 57, 60, 72, 76]
69
+ violation_lines = [63, 64, 73, 76, 88, 92, 97, 98, 101, 106, 107, 110]
96
70
  compare_warning_with_lines(violation_lines)
97
71
  end
98
72
 
99
- #
100
- # Test for waning texts and links.
101
- #
102
-
103
- it "Log with variables is warned with link address" do
104
- warning_link = "http://error.io"
105
- allow(@logging_lint.git).to receive(:added_files).and_return(added_files)
106
- allow(@logging_lint).to receive(:warning_description).and_return(warning_link)
73
+ it "Log with variables is warned for new files" do
74
+ allow(@logging_lint.git).to receive(:added_files).and_return(ADDED_FILES)
107
75
  @logging_lint.log_lint
108
- expect(@dangerfile.status_report[:warnings][0]).to eq("#{warning_text} #{warning_link}")
76
+ violation_lines = [47, 48, 57, 60, 72, 76]
77
+ compare_warning_with_lines(violation_lines)
109
78
  end
110
79
 
111
- it "Log with variables is warned without link address" do
112
- allow(@logging_lint.git).to receive(:added_files).and_return(added_files)
80
+ it "Log with variables is warned for new files (with all params)" do
81
+ custom_warning_text = "Warning text"
82
+ allow(@logging_lint.git).to receive(:added_files).and_return(ADDED_FILES)
83
+ @logging_lint.log_functions = %w(logInfo)
84
+ @logging_lint.warning_text = custom_warning_text
85
+ @logging_lint.log_regex = '[ ]?[{(](?:\n?|.)["]?(?:\n?|.)["]?(?:\n?|.)+(?:[)}][ ]?\n)'
86
+ @logging_lint.line_variable_regex = ['[{(](\n| |\+)*([^\"]\w[^\"])+', '(\".*\$.*\")']
87
+ @logging_lint.line_remove_regex = ['(\+ )?\".*\"']
113
88
  @logging_lint.log_lint
114
- expect(@dangerfile.status_report[:warnings][0]).to eq(warning_text)
89
+ violation_lines = [47, 48, 57, 60, 72, 76]
90
+ compare_warning_with_lines(violation_lines)
91
+ expect(@dangerfile.violation_report[:warnings][0].message).to eq(custom_warning_text)
115
92
  end
116
93
 
117
94
  #
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("spec_helper", __dir__)
4
+
5
+ #
6
+ # Tests for warning text creation. Text can contain description which can be defined in Danger file.
7
+ #
8
+
9
+ module Danger
10
+ describe Danger::DangerLoggingLint do
11
+ #
12
+ # Defines linter, danger file and other variables used by the linter.
13
+ #
14
+ describe "with Dangerfile" do
15
+ before do
16
+ @dangerfile = testing_dangerfile
17
+ @logging_lint = @dangerfile.logging_lint
18
+
19
+ mock_variables(@logging_lint)
20
+ end
21
+
22
+ #
23
+ # Test for waning text and description (optional).
24
+ #
25
+
26
+ it "Log with variables is warned description (link address)" do
27
+ warning_description = "http://error.io"
28
+ allow(@logging_lint.git).to receive(:added_files).and_return(ADDED_FILES)
29
+ allow(@logging_lint).to receive(:warning_description).and_return(warning_description)
30
+ @logging_lint.log_lint
31
+ expect(@dangerfile.status_report[:warnings][0]).to eq("#{WARNING_TEXT} #{warning_description}")
32
+ end
33
+
34
+ it "Log with variables is warned without warning description" do
35
+ allow(@logging_lint.git).to receive(:added_files).and_return(ADDED_FILES)
36
+ @logging_lint.log_lint
37
+ expect(@dangerfile.status_report[:warnings][0]).to eq(WARNING_TEXT)
38
+ end
39
+ end
40
+ end
41
+ end
data/spec/spec_helper.rb CHANGED
@@ -71,3 +71,22 @@ def testing_dangerfile
71
71
  env = Danger::EnvironmentManager.new(testing_env)
72
72
  Danger::Dangerfile.new(env, testing_ui)
73
73
  end
74
+
75
+ # Mocks linter variables. Should be called in "before" block.
76
+ def mock_variables(logging_lint)
77
+ allow(logging_lint.git).to receive(:deleted_files).and_return([])
78
+ allow(logging_lint.git).to receive(:added_files).and_return([])
79
+ allow(logging_lint.git).to receive(:modified_files).and_return([])
80
+ allow(logging_lint).to receive(:file_extensions).and_return(%w(kt))
81
+ allow(logging_lint).to receive(:log_functions).and_call_original
82
+ allow(logging_lint).to receive(:warning_text).and_call_original
83
+ allow(logging_lint).to receive(:log_regex).and_call_original
84
+ allow(logging_lint).to receive(:line_variable_regex).and_call_original
85
+ allow(logging_lint).to receive(:line_remove_regex).and_call_original
86
+ end
87
+
88
+ # Defines test variables used in multiple text files.
89
+ DIR_NAME = File.dirname(__FILE__)
90
+ MODIFIED_FILES = %W(#{DIR_NAME}/fixtures/ModifiedFile.kt #{DIR_NAME}/fixtures/IgnoredModifiedFile.txt).freeze
91
+ ADDED_FILES = %W(#{DIR_NAME}/fixtures/NewFile.kt).freeze
92
+ WARNING_TEXT = "Does this log comply with security rules?"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger-logging_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Sucharda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-22 00:00:00.000000000 Z
11
+ date: 2022-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api
@@ -192,7 +192,9 @@ files:
192
192
  - spec/fixtures/IgnoredModifiedFile.txt
193
193
  - spec/fixtures/ModifiedFile.kt
194
194
  - spec/fixtures/NewFile.kt
195
+ - spec/logging_lint_no_run_spec.rb
195
196
  - spec/logging_lint_spec.rb
197
+ - spec/logging_lint_text_spec.rb
196
198
  - spec/spec_helper.rb
197
199
  homepage: https://github.com/eManPrague/danger-logging_lint
198
200
  licenses:
@@ -222,5 +224,7 @@ test_files:
222
224
  - spec/fixtures/IgnoredModifiedFile.txt
223
225
  - spec/fixtures/ModifiedFile.kt
224
226
  - spec/fixtures/NewFile.kt
227
+ - spec/logging_lint_no_run_spec.rb
225
228
  - spec/logging_lint_spec.rb
229
+ - spec/logging_lint_text_spec.rb
226
230
  - spec/spec_helper.rb