banalize 0.0.2 → 0.0.3

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: 87630dc6d2b7488f3fb027555a11fa8a55e51e69
4
- data.tar.gz: 7125e23ea24ff1e1d76755431cc89e20a639c1b8
3
+ metadata.gz: 2b0087481450fe823d7653961d51e65a77d1c204
4
+ data.tar.gz: d0c17b02bfb921f7b9c66f30891b24cd48cc1606
5
5
  SHA512:
6
- metadata.gz: 8e3f424b7c271325b8824af7be082dcadc94960045389d0d668277afd5e9d36ed61e41a6a1ff2780d234755dd55918c7d2463b3a184a2a953dca208df56db20c
7
- data.tar.gz: e6c02e8669009ae01a06b6e6b42622909227ae576496c5e2826189ea70802dbe12d852bf39292bdead538c9de93ca661458c1b514e9e8acc74ff6d3399b2a604
6
+ metadata.gz: 11e7fe1cc961b17e79fab3d8d3c3db3fb9fa36912d7664330c013b12cdb0b7f1d797c7b6a66dfc9df74cd390c2194c414b31a05db8ae4158f2a83204a7edd0c7
7
+ data.tar.gz: 4c417850b591635872e6b272b781dda161e4a06cdb8ff9a369f070e8cd161e01f621c169c002fa5fff2ff845e9aa91986036ce40692ecaca7b67d2cf5efaac45
data/History.md CHANGED
@@ -1,3 +1,16 @@
1
+ ## v.0.0.3
2
+
3
+ * Thu Mar 28 2013 -- Dmytro Kovalov
4
+
5
+ - Filtering files by extension list: comma separated
6
+ - Add default sorting to policy search
7
+ - Policy to check braces ${a} around variables
8
+ with test for it
9
+ - Add template for ruby policy in the docs directory
10
+ - Policy to check uninitialized variables
11
+ - Currently processed bash: Add error reporting on exit of currently processed bash file.
12
+ - ShellVariables module for parser
13
+ Find all shell variables in script into array and accessor `variables`
1
14
 
2
15
  ## v.0.0.2
3
16
 
@@ -60,7 +60,8 @@ pre do |global,command,options,args|
60
60
  # - results of checks
61
61
  # - count of failed checks
62
62
  # - total count of checks
63
- $res, $status, $total = { }, 0, 0
63
+ # - current - name of the currently processed bash file
64
+ $res, $status, $total, $current = { }, 0, 0, ''
64
65
  true
65
66
  end
66
67
 
@@ -115,6 +116,7 @@ end
115
116
 
116
117
  on_error do |exception|
117
118
  unless ENV['GLI_DEBUG'] == 'true'
119
+ puts "Current bash file #{$current}"
118
120
  exit_now! exception.message unless exception.is_a? GLI::BadCommandLine
119
121
  end
120
122
  true
@@ -1,6 +1,7 @@
1
1
  module Banalize
2
2
 
3
3
  require_relative 'parser/pod_comments'
4
+ require_relative 'parser/variables'
4
5
 
5
6
  # Instance attributes
6
7
  # -----------
@@ -14,14 +15,16 @@ module Banalize
14
15
 
15
16
  class Parser
16
17
 
17
- include Banalize::Parser::PodStyleComments
18
+ include PodStyleComments
19
+ include ShellVariables
18
20
 
19
21
  def initialize path
20
- @lines = IO.read(path).force_encoding("utf-8").split($/)
21
- @shebang = Numbered.new
22
- @comments = Numbered.new
23
- @code = Numbered.new
24
-
22
+ @lines = IO.read(path).force_encoding("utf-8").split($/)
23
+ @shebang = Numbered.new
24
+ @comments = Numbered.new
25
+ @code = Numbered.new
26
+ @variables = []
27
+
25
28
  @shebang.add @lines.shift if @lines.first =~ /^#!/
26
29
 
27
30
  @lines.each_index do |idx|
@@ -37,7 +40,7 @@ module Banalize
37
40
  end
38
41
  end
39
42
  pod_comments
40
-
43
+ shell_variables
41
44
  end
42
45
 
43
46
  # Lines of the tested bash file, split by \n's
@@ -0,0 +1,30 @@
1
+ module Banalize
2
+ class Parser
3
+
4
+ ##
5
+ # Parse and detect all shell variables used in script. Set
6
+ # instance level accessor `variables`.
7
+ #
8
+ module ShellVariables
9
+
10
+ ##
11
+ # Parse and detect all shell variables used in script.
12
+ #
13
+ # @return [Array]
14
+ def shell_variables
15
+ ln = code.grep(/\$\{?\w+\}?/).map(&:last).join " "
16
+
17
+ vars = ln.scan(/\$\{?\w+\}?/)
18
+ vars.map! { |x| x.gsub(/[${}]/,'') }
19
+ vars.reject! { |x| x =~ /^\d$/}
20
+
21
+ @variables = vars || []
22
+ @variables.uniq!
23
+ end
24
+
25
+ # All variables used in shell script
26
+ attr_accessor :variables
27
+
28
+ end # ShellVariables
29
+ end # Parser
30
+ end # Banalize
@@ -78,7 +78,7 @@ module Banalize
78
78
  else
79
79
  raise ArgumentError, "Unknown search criteria: #{search.inspect}"
80
80
 
81
- end.compact
81
+ end.compact.sort { |a,b| a[:policy] <=> b[:policy] } # By default sort by policy name
82
82
  end
83
83
 
84
84
  ##
@@ -14,6 +14,8 @@ module Banalize
14
14
  #
15
15
  def self.run bash, search
16
16
 
17
+ $current = bash
18
+
17
19
  run_list = Policy.search search
18
20
 
19
21
  if run_list.empty?
@@ -3,20 +3,14 @@ desc 'Banalize file(s) from single or multiple directories. Can use wildcards an
3
3
  arg_name 'dir', :multiple
4
4
  command [:directory, :dir] do |c|
5
5
 
6
- c.desc "Show all results, by default only failures shown (only for long format)"
7
- c.switch [:a,:all]
8
6
 
9
- c.desc "Short dotted output format"
10
- c.switch [:s, :short, :dots]
7
+ c.switch [:a,:all], desc: "Show all results, not only failures (for long format)"
8
+ c.switch [:dots, :d], desc: "Short dotted output format"
9
+ c.switch [:recursive, :recur, :r], desc: "Recursive scan directories for files"
10
+ c.switch [:allow_files, :f], desc: "Allow use of file paths together with directory paths"
11
11
 
12
- c.desc "Recursive scan directories for files"
13
- c.switch [:recursive, :recur, :r]
14
-
15
- c.switch [:allow_files, :f], :desc => "Allow use of file paths together with directory paths"
16
-
17
- c.desc "Wildcard for file lists"
18
- c.default_value "*"
19
- c.flag [:wildcard, :w]
12
+ c.flag [:wildcard, :w], desc: "Wildcard for file lists", default_value: "*"
13
+ c.flag [:except, :ex], desc: "Skip files with listed extensions (comma-separated)"
20
14
 
21
15
  c.desc "With 'no-' do not show errors, only name of failed check"
22
16
  c.default_value true
@@ -32,8 +26,23 @@ command [:directory, :dir] do |c|
32
26
  files += Dir.glob("#{dir}/#{ options[:r] ? '**/' : ''}#{options[:wildcard]}").select { |x| File.file? x}
33
27
  end
34
28
  }
29
+ ##
30
+ # Filter out files by extentions
31
+ #
32
+ if options[:except]
33
+
34
+ extensions = options[:except]
35
+
36
+ extensions = extensions.split(/\s*,\s*/) if
37
+ extensions.is_a? String
38
+
39
+ extensions.each do |ext|
40
+ files.reject! { |file| file =~ /.*\.#{ext}/ }
41
+ end
42
+ end
43
+
35
44
  files.each { |file| $res[file] = Banalize.run(file, $search) }
45
+
36
46
  end
37
47
  end
38
48
 
39
-
@@ -2,12 +2,10 @@ desc 'Run banalize on a single file or multiple files'
2
2
 
3
3
  arg_name 'filename', :multiple
4
4
  command [:file, :fl] do |c|
5
-
6
- c.desc "Show all results, by default only failures shown (only for long format)"
7
- c.switch [:a,:all]
8
5
 
9
- c.desc "Short dotted output format"
10
- c.switch [:s, :short, :dots]
6
+
7
+ c.switch [:a,:all], desc: "Show all results, not only failures (for long format)"
8
+ c.switch [:dots, :d], desc: "Short dotted output format"
11
9
 
12
10
  c.desc "With 'no-' do not show errors, only name of failed check"
13
11
  c.default_value true
@@ -0,0 +1,37 @@
1
+ banalizer File.basename(__FILE__, '.rb').to_sym do
2
+
3
+ synopsis 'Always use braces to isolate variables ${a}'
4
+ severity :stern
5
+ style :bugs
6
+
7
+ description <<-DESC
8
+
9
+ Use braces around variables
10
+ ---------------------------
11
+
12
+ To prevent wrong expansion of variables always use braces around
13
+ variables. I.e.
14
+
15
+ Good: ${VARIABLE}
16
+ Bad: $VARIABLE
17
+
18
+ foo=sun
19
+ echo $fooshine # $fooshine is undefined
20
+ echo ${foo}shine # displays the word "sunshine"
21
+
22
+ DESC
23
+
24
+ def run
25
+
26
+ variables.each do |var|
27
+
28
+ if code.has?(/\$#{var}/)
29
+ errors.add "Variable $#{var} used without braces"
30
+ errors.add " Lines: #{code.lines}"
31
+ end
32
+ end
33
+
34
+ errors.empty?
35
+ end
36
+
37
+ end
@@ -15,7 +15,7 @@ banalizer File.basename(__FILE__, '.rb').to_sym do
15
15
 
16
16
  else
17
17
  pct = ((comments.size.to_f / code.size) * 100).to_i
18
- errors.add "Code commented on #{pct}%" if pct < default[:percent]
18
+ errors.add "Code comment coverage #{pct}%" if pct < default[:percent]
19
19
  end
20
20
 
21
21
  errors.empty?
@@ -5,13 +5,14 @@ banalizer :explicitly_define_path_variable do
5
5
 
6
6
  description <<-EOF
7
7
 
8
- PATH varaible should be defined explicitly in the script. It should *only* list absolute path names and does not have $PATH variable.
8
+ PATH varaible should be defined explicitly in the script. It should
9
+ *only* list absolute path names and does not have $PATH variable.
9
10
 
10
- Quote from http://hub.opensolaris.org/bin/view/Community+Group+on/shellstyle#HPathnames
11
+ Ref.: http://hub.opensolaris.org/bin/view/Community+Group+on/shellstyle#HPathnames
11
12
 
12
- It is always a good idea to be careful about $PATH settings and
13
- pathnames when writing shell scripts. This allows them to function
14
- correctly even when the user invoking your script has some strange
13
+ It is always a good idea to be careful about $PATH settings and
14
+ pathnames when writing shell scripts. This allows them to function
15
+ correctly even when the user invoking your script has some strange
15
16
  $PATH set in their environment.
16
17
 
17
18
  There are two acceptable ways to do this:
@@ -27,13 +28,11 @@ PATH=/usr/bin; export PATH
27
28
  chown root bar
28
29
  chgrp sys bar
29
30
 
30
- DO NOT use a mixture of (1) and (2) in the same script. Pick one method and use it consistently.
31
-
31
+ DO NOT use a mixture of (1) and (2) in the same script. Pick one
32
+ method and use it consistently.
32
33
 
33
34
  EOF
34
35
 
35
-
36
-
37
36
  parser :bash
38
37
 
39
38
  def run
@@ -0,0 +1,45 @@
1
+ banalizer File.basename(__FILE__, '.rb').to_sym do
2
+
3
+ synopsis 'Variables should be explicitly initialized'
4
+ severity :gentle
5
+ style :security
6
+
7
+ description <<-DESC
8
+
9
+ Variable initialization
10
+ ------------------------
11
+
12
+ As in C, it's always a good idea to initialize your variables, though,
13
+ the shell will initialize fresh variables itself (better: Unset
14
+ variables will generally behave like variables containing a
15
+ nullstring).
16
+
17
+ It's no problem to pass a variable you use as environment to the
18
+ script. If you blindly assume that all variables you use are empty for
19
+ the first time, somebody can inject a variable content by just passing
20
+ it in the environment.
21
+
22
+ The solution is simple and effective: Initialize them
23
+
24
+ my_input=""
25
+ my_array=()
26
+ my_number=0
27
+
28
+ If you do that for every variable you use, then you also have a kind
29
+ of documentation for them.
30
+
31
+ Ref: http://wiki.bash-hackers.org/scripting/style#variable_initialization
32
+
33
+ DESC
34
+
35
+ def run
36
+
37
+ variables.each do |var|
38
+ errors.add "Variable ${#{var}} used without initializing" if
39
+ code.dont_have? /#{var}=/
40
+ end
41
+
42
+ errors.empty?
43
+ end
44
+
45
+ end
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmytro Kovalov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-27 00:00:00.000000000 Z
11
+ date: 2013-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -84,6 +84,7 @@ files:
84
84
  - ./lib/banalize/files.rb
85
85
  - ./lib/banalize/parser/numbered.rb
86
86
  - ./lib/banalize/parser/pod_comments.rb
87
+ - ./lib/banalize/parser/variables.rb
87
88
  - ./lib/banalize/parser.rb
88
89
  - ./lib/banalize/policy/severity.rb
89
90
  - ./lib/banalize/policy.rb
@@ -96,6 +97,7 @@ files:
96
97
  - ./lib/commands/list.rb
97
98
  - ./lib/core_extensions/string.rb
98
99
  - ./lib/helpers/beautify.rb
100
+ - ./lib/policies/braces_for_variables.rb
99
101
  - ./lib/policies/comment_coverage.rb
100
102
  - ./lib/policies/consistent_indents.rb
101
103
  - ./lib/policies/define_path.rb
@@ -105,6 +107,7 @@ files:
105
107
  - ./lib/policies/max_line_length.rb
106
108
  - ./lib/policies/shebang_format.rb
107
109
  - ./lib/policies/trailing_spaces.rb
110
+ - ./lib/policies/uninitialized_variables.rb
108
111
  - ./lib/policies/minus_n_syntax_check
109
112
  homepage: http://wizcorp.jp
110
113
  licenses: []