mago 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
  SHA1:
3
- metadata.gz: bff22636bac0787bd8a88f2889e8f076f4f5fe61
4
- data.tar.gz: 2073a8323b25fd5ad67b771181513bc5c135977e
3
+ metadata.gz: 069e929006e5cab92bbebe28e6eca37476916809
4
+ data.tar.gz: 44440710c44bbffecc85f180bbbe96869e03e2b5
5
5
  SHA512:
6
- metadata.gz: 6cfab0b16c68d008dcf9564bc2233297e1d145e46e42629441ca12a9ac6cfff2cba9be3e4e1029da0a75d5700959bee795992572b67f23cef474884bb4ca63e4
7
- data.tar.gz: 872e646954a12c6f9eed35b5d3ceb6bffff97bd73a4c5e055b019516fd2f1dc7f685425782ca71c059027f54f0c0027ff1064b085e95ea1c7060c3d76bd8981c
6
+ metadata.gz: cebe0d24c4832b0a43669da316276faf4fc5204a1e1a3f79fc37ee7403a9ffd0bea4be21d0f9965bc58f30545a759f9aeb88813fa03ab952866e62f5239e7579
7
+ data.tar.gz: 1e7f38b8af6c6ccbc10fe85bb117f670ed5899880a2b2b69a7e3773aec2d4397d146bd5d6575f88fc2474da5f4b6ba7a3a76526d7e6f5915d37f8a489c3f35cd
@@ -9,20 +9,7 @@ Tool to detect magic numbers in ruby code.
9
9
  gem install mago
10
10
  ```
11
11
 
12
-
13
- ## Usage
14
-
15
- Detect magic numbers in particular ruby file:
16
- ```sh
17
- mago ./path/to/file.rb
18
- ```
19
-
20
- In all ruby files inside directory:
21
- ```sh
22
- mago ./path/to/project/
23
- ```
24
-
25
- ## Example
12
+ ## Usage example
26
13
 
27
14
  Ruby code in `square.rb`:
28
15
  ```ruby
@@ -39,6 +26,23 @@ mago ./square.rb
39
26
  ./square.rb:4 detected magic number 2
40
27
  ```
41
28
 
29
+ ### Ignoring specific numbers
30
+
31
+ Use `--ignore` or `-i` option to ignore specific numbers. By default 0 and 1 are ignored.
32
+
33
+ ```sh
34
+ mago -i 2,3 ./square.rb
35
+ ./square.rb:3 detected magic number 5
36
+ ```
37
+
38
+ ### Color output
39
+
40
+ Use `--color` or `-c` option to colorize output.
41
+
42
+ ## TODO
43
+
44
+ * Support for `--show-source`(`-s`) option to show line of source code with magic number.
45
+ * Support for `--expl-var` (`-e`) option to ignore explaining variables.
42
46
 
43
47
  ## Copyright
44
48
 
data/bin/mago CHANGED
@@ -3,46 +3,6 @@
3
3
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
4
 
5
5
  require 'mago'
6
+ require 'mago/cli'
6
7
 
7
- def run(args)
8
- case args.first
9
- when '--help', '-h'
10
- show_help
11
- when '--version', '-v'
12
- show_version
13
- else
14
- run_mago(args)
15
- end
16
- end
17
-
18
- def run_mago(args)
19
-
20
- if args.empty?
21
- args << '.'
22
- end
23
-
24
- ruby_files = Mago::FileFinder.new(args).find
25
- processor = Mago::Detector.new(ruby_files, :ignore => [0, 1])
26
- report = processor.run
27
- formatter = Mago::Formatter.new(report)
28
- puts formatter.format
29
- end
30
-
31
- def show_help
32
- puts "Description:\n" \
33
- " Magic numbers detector for ruby\n\n"\
34
- "Usage:\n" \
35
- " mago # inspect ruby files in current directory\n" \
36
- " mago ./my_code.rb # inspect ./my_code.rb file\n" \
37
- " mago ./my_project/ # inspect ruby files in ./my_project/"
38
- end
39
-
40
-
41
- def show_version
42
- puts "Mago #{Mago::VERSION}\n" \
43
- "Tool to detect magic numbers in ruby files.\n" \
44
- "Copyright (c) 2013 Sergey Potapov"
45
- end
46
-
47
-
48
- run(ARGV)
8
+ Mago::Cli::Command.new(ARGV).execute
@@ -6,7 +6,6 @@ require 'mago/ruby_file'
6
6
  require 'mago/sexp_processor'
7
7
  require 'mago/detector'
8
8
  require 'mago/report'
9
- require 'mago/formatter'
10
9
  require 'mago/file_finder'
11
10
  require 'mago/version'
12
11
 
@@ -0,0 +1,9 @@
1
+ require 'mago/cli/colorize'
2
+ require 'mago/cli/command'
3
+ require 'mago/cli/formatter'
4
+
5
+ module Mago
6
+ module Cli
7
+
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module Mago
2
+ module Cli
3
+ module Colorize
4
+ def colorize(text, color_code)
5
+ "\e[#{color_code}m#{text}\e[0m"
6
+ end
7
+
8
+ def red(text)
9
+ colorize(text, 31)
10
+ end
11
+
12
+ def green(text)
13
+ colorize(text, 32)
14
+ end
15
+
16
+ def yellow(text)
17
+ colorize(text, 33)
18
+ end
19
+
20
+ def pink(text)
21
+ colorize(text, 35)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,83 @@
1
+ module Mago
2
+ module Cli
3
+ class Config
4
+ attr_accessor :ignore, :files, :color
5
+
6
+ def initialize
7
+ @files = []
8
+ @color = false
9
+ end
10
+ end
11
+
12
+ class Command
13
+ def initialize(args)
14
+ @args = args
15
+ @config = Config.new
16
+ end
17
+
18
+ def execute
19
+ parse_args
20
+ run
21
+ end
22
+
23
+ def parse_args
24
+ while arg = @args.shift
25
+ case arg
26
+ when '--help', '-h'
27
+ show_help
28
+ exit 0
29
+ when '--version', '-v'
30
+ show_version
31
+ exit 0
32
+ when '--ignore', '-i'
33
+ val = @args.shift
34
+ abort "--ignore option requires comma separated numbers" unless val =~ /^[0-9]/
35
+ nums = val.to_s.split(',').map{ |n| n.include?('.') ? n.to_f : n.to_i }
36
+ @config.ignore = nums
37
+ when '--color', '--colour', '-c'
38
+ @config.color = true
39
+ when /^-/
40
+ abort("Unknown option `#{arg}'")
41
+ else
42
+ @config.files << arg
43
+ end
44
+ end
45
+
46
+ @config.files << '.' if @config.files.empty?
47
+ end
48
+
49
+
50
+ private
51
+
52
+ def run
53
+ ruby_files = Mago::FileFinder.new(@config.files).find
54
+ detector = Mago::Detector.new(ruby_files, :ignore => @config.ignore)
55
+ report = detector.run
56
+ formatter = Mago::Cli::Formatter.new(report, :color => @config.color)
57
+
58
+ puts formatter.format
59
+ end
60
+
61
+ def show_help
62
+ puts "Magic numbers detector for Ruby programming language.\n\n" \
63
+ " Syntax:\n" \
64
+ " mago [OPTIONS] [FILES]\n\n" \
65
+ " Options:\n" \
66
+ " -i, --ignore NUMS\n" \
67
+ " Comma separated numbers, which will be ignored. Default is 0,1\n\n" \
68
+ " Usage:\n" \
69
+ " mago # inspect ruby files in current directory\n" \
70
+ " mago ./my_code.rb # inspect ./my_code.rb file\n" \
71
+ " mago ./my_project/ # inspect ruby files in ./my_project/"
72
+ end
73
+
74
+
75
+ def show_version
76
+ puts "Mago #{Mago::VERSION}\n" \
77
+ "Tool to detect magic numbers in ruby files.\n" \
78
+ "Copyright (c) 2013 Sergey Potapov"
79
+ end
80
+
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,47 @@
1
+ module Mago
2
+ module Cli
3
+ class Formatter
4
+ include Colorize
5
+
6
+ def initialize(report, opts = {})
7
+ @report = report
8
+
9
+ @color = opts[:color]
10
+ end
11
+
12
+ def format
13
+ out = ''
14
+
15
+ @report.files.each do |file|
16
+ format_file(file, out)
17
+ end
18
+
19
+ @report.errors.each do |error|
20
+ format_error(error, out)
21
+ end
22
+
23
+ out
24
+ end
25
+
26
+ def format_file(file, out)
27
+ file.magic_numbers.each do |num|
28
+ if @color
29
+ val = red(num.value)
30
+ line = yellow(num.line)
31
+ path = pink(file.path)
32
+ else
33
+ val = num.value
34
+ line = num.line
35
+ path = file.path
36
+ end
37
+
38
+ out << "#{path}:#{line} detected magic number #{val}\n"
39
+ end
40
+ end
41
+
42
+ def format_error(error, out)
43
+ out << "ERROR: %s\n" % [error]
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,9 +1,12 @@
1
1
  module Mago
2
2
  class Detector
3
+ # Numbers which are ignored by default
4
+ DEFAULT_IGNORE = [0, 1]
5
+
3
6
  def initialize(file_paths = [], options = {})
4
7
  @file_paths = file_paths
5
8
  @report = Report.new
6
- @ignore = options[:ignore] || []
9
+ @ignore = options[:ignore] || DEFAULT_IGNORE
7
10
  end
8
11
 
9
12
  def run
@@ -5,18 +5,18 @@ module Mago
5
5
  end
6
6
 
7
7
  def find
8
- file_paths = []
8
+ ruby_files = []
9
9
 
10
10
  @paths.each do |path|
11
11
  if File.directory?(path)
12
- dir_files = Dir["#{path}/**/*.rb"]
13
- file_paths.concat(dir_files)
12
+ pattern = File.join(path, '/**/*.rb')
13
+ ruby_files.concat(Dir[pattern])
14
14
  else
15
- file_paths << path
15
+ ruby_files << path
16
16
  end
17
17
  end
18
18
 
19
- file_paths
19
+ ruby_files
20
20
  end
21
21
  end
22
22
  end
@@ -1,3 +1,3 @@
1
1
  module Mago
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -0,0 +1 @@
1
+ f - - -
@@ -0,0 +1,6 @@
1
+ puts 0.0
2
+ puts 1.0
3
+ puts 1.0
4
+ puts 2.0
5
+ puts 3.0
6
+ puts 5.0
@@ -0,0 +1,6 @@
1
+ P = 3.14
2
+
3
+ radius = 5 - 1
4
+ square = P * radius ** 2
5
+
6
+ raise("Wow!") if square < 0
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'mago command' do
4
+ def mago(args = '')
5
+ cmd = "cd #{FIXTURES_PATH} && #{MAGO_BIN} #{args}"
6
+ %x"#{cmd}"
7
+ end
8
+
9
+ describe 'arguments' do
10
+ it 'without arguments should look through ruby files in the current directory' do
11
+ output = mago('')
12
+
13
+ output.should include('./square.rb')
14
+ output.should include('./math/fibonacci.rb')
15
+ output.should include('./invalid.rb')
16
+ end
17
+
18
+ it 'when directory passed should inspect all ruby files inside' do
19
+ output = mago('./math/')
20
+
21
+ output.should include('./math/fibonacci.rb')
22
+ output.should_not include('square')
23
+ end
24
+ end
25
+
26
+ context 'ruby file with invalid syntax' do
27
+ it 'should report files with syntax errors' do
28
+ output = mago('./invalid.rb')
29
+ output.should include('ERROR: ./invalid.rb has invalid ruby code')
30
+ end
31
+ end
32
+
33
+
34
+ describe 'options' do
35
+ context 'without options' do
36
+ it 'should detect magic number except 0 and 1' do
37
+ mago('./square.rb').should ==
38
+ "./square.rb:3 detected magic number 5\n" \
39
+ "./square.rb:4 detected magic number 2\n"
40
+ end
41
+ end
42
+
43
+ describe '--ignore option' do
44
+ it 'should ignore passed numbers' do
45
+ expected = "./square.rb:3 detected magic number 5\n"\
46
+ "./square.rb:3 detected magic number 1\n"
47
+
48
+ mago('--ignore 0,2 ./square.rb').should == expected
49
+ mago('-i 0,2 ./square.rb').should == expected
50
+ end
51
+ end
52
+ end
53
+ end
@@ -7,6 +7,9 @@ require 'mago'
7
7
  # in ./support/ and its subdirectories.
8
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
9
 
10
+ FIXTURES_PATH = File.expand_path('../fixtures', __FILE__)
11
+ MAGO_BIN = File.expand_path('../../bin/mago', __FILE__)
12
+
10
13
  RSpec.configure do |config|
11
-
14
+
12
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mago
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
  - Sergey Potapov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-09 00:00:00.000000000 Z
11
+ date: 2013-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby_parser
@@ -55,14 +55,19 @@ files:
55
55
  - ./lib/mago/ruby_file.rb
56
56
  - ./lib/mago/sexp_processor.rb
57
57
  - ./lib/mago/detector.rb
58
- - ./lib/mago/formatter.rb
58
+ - ./lib/mago/cli.rb
59
+ - ./lib/mago/cli/formatter.rb
60
+ - ./lib/mago/cli/command.rb
61
+ - ./lib/mago/cli/colorize.rb
59
62
  - ./lib/mago/magic_number.rb
60
63
  - ./bin/mago
61
64
  - LICENSE.txt
62
65
  - README.markdown
63
- - ./spec/mago_spec.rb
64
66
  - ./spec/spec_helper.rb
65
- - ./spec/fixtures/code.rb
67
+ - ./spec/fixtures/math/fibonacci.rb
68
+ - ./spec/fixtures/square.rb
69
+ - ./spec/fixtures/invalid.rb
70
+ - ./spec/integration/cli_spec.rb
66
71
  - bin/mago
67
72
  homepage: https://github.com/greyblake/mago
68
73
  licenses:
@@ -89,7 +94,9 @@ signing_key:
89
94
  specification_version: 4
90
95
  summary: Tool to detect magic numbers in ruby code
91
96
  test_files:
92
- - ./spec/mago_spec.rb
93
97
  - ./spec/spec_helper.rb
94
- - ./spec/fixtures/code.rb
98
+ - ./spec/fixtures/math/fibonacci.rb
99
+ - ./spec/fixtures/square.rb
100
+ - ./spec/fixtures/invalid.rb
101
+ - ./spec/integration/cli_spec.rb
95
102
  has_rdoc:
@@ -1,31 +0,0 @@
1
- module Mago
2
- class Formatter
3
- def initialize(report)
4
- @report = report
5
- end
6
-
7
- def format
8
- out = ''
9
-
10
- @report.files.each do |file|
11
- format_file(file, out)
12
- end
13
-
14
- @report.errors.each do |error|
15
- format_error(error, out)
16
- end
17
-
18
- out
19
- end
20
-
21
- def format_file(file, out)
22
- file.magic_numbers.each do |num|
23
- out << "%s:%d detected magic number %s\n" % [file.path, num.line, num.value]
24
- end
25
- end
26
-
27
- def format_error(error, out)
28
- out << "ERROR: %s\n" % [error]
29
- end
30
- end
31
- end
@@ -1,10 +0,0 @@
1
- P = 3.14
2
-
3
- puts :a
4
-
5
- a = 2
6
- if a > 10.2
7
- puts "Hello"
8
- end
9
-
10
- fds
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Mago" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end