tree.rb 0.3.11 → 0.3.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/bin/rjson.rb +1 -2
  2. data/bin/rtree +1 -3
  3. data/bin/tree.rb +1 -3
  4. data/bin/tree_rb +1 -3
  5. data/examples/d3js_layout_partition/run.sh +0 -0
  6. data/examples/d3js_layout_treemap/index.html +0 -0
  7. data/examples/d3js_layout_treemap/run.sh +0 -0
  8. data/examples/d3js_layout_treemap/style.css +0 -0
  9. data/lib/tree_rb.rb +6 -6
  10. data/lib/tree_rb/cli/cli_json.rb +11 -215
  11. data/lib/tree_rb/cli/cli_tree.rb +103 -85
  12. data/lib/tree_rb/core/tree_node.rb +8 -7
  13. data/lib/tree_rb/core/tree_node_visitor.rb +13 -6
  14. data/lib/tree_rb/extension_numeric.rb +4 -4
  15. data/lib/tree_rb/{input_file_system → input_plugins/file_system}/dir_processor.rb +1 -1
  16. data/lib/tree_rb/{input_file_system → input_plugins/file_system}/directory_walker.rb +17 -9
  17. data/lib/tree_rb/{input_html_page → input_plugins/html_page}/dom_walker.rb +0 -0
  18. data/lib/tree_rb/{output_dircat/dircat_helper.rb → output_plugins/dircat/dircat_output.rb} +5 -5
  19. data/lib/tree_rb/{output_dircat → output_plugins/dircat}/dircat_visitor.rb +0 -0
  20. data/lib/tree_rb/{output_dircat → output_plugins/dircat}/entry.rb +1 -1
  21. data/lib/tree_rb/{output_html/d3js_helper.rb → output_plugins/html/d3js_output.rb} +5 -4
  22. data/lib/tree_rb/{output_html → output_plugins/html}/directory_to_hash2_visitor.rb +0 -0
  23. data/lib/tree_rb/{output_html → output_plugins/html}/erb_render.rb +0 -0
  24. data/lib/tree_rb/{output_sqlite → output_plugins/sqlite}/sqlite_dir_tree_visitor.rb +9 -4
  25. data/lib/tree_rb/{output_sqlite/sqlite_helper.rb → output_plugins/sqlite/sqlite_output.rb} +11 -9
  26. data/lib/tree_rb/version.rb +1 -1
  27. data/lib/tree_rb/visitors/print_tree_node_visitor.rb +4 -4
  28. data/spec/fixtures/test_dir_1/.dir_with_dot/dummy.txt +1 -0
  29. data/spec/fixtures/test_dir_2/[Dsube]/sub/.gitkeep +0 -0
  30. data/spec/fixtures/tmp/test.db +0 -0
  31. data/spec/spec_helper.rb +2 -2
  32. data/spec/tree_rb/cli/cli_json_spec.rb +5 -5
  33. data/spec/tree_rb/cli/cli_tree_d3js_spec.rb +3 -3
  34. data/spec/tree_rb/cli/cli_tree_generic_spec.rb +126 -95
  35. data/spec/tree_rb/cli/cli_tree_sqlite_spec.rb +38 -22
  36. data/spec/tree_rb/core/tree_node_spec.rb +135 -135
  37. data/spec/tree_rb/input_file_system/dir_processor_spec.rb +3 -3
  38. data/spec/tree_rb/input_file_system/directory_walker_conf_spec.rb +59 -0
  39. data/spec/tree_rb/input_file_system/directory_walker_run_spec.rb +62 -0
  40. data/tasks/rspec.rake +2 -2
  41. data/tasks/tree_rb.rake +2 -2
  42. data/tasks/yard.rake +1 -1
  43. data/tree.rb.gemspec +1 -1
  44. metadata +121 -108
  45. checksums.yaml +0 -7
  46. data/lib/colors.rb +0 -39
  47. data/spec/fixtures/tmp/dircat +0 -34
  48. data/spec/tree_rb/input_file_system/directory_walker_spec.rb +0 -98
@@ -266,7 +266,7 @@ module TreeRb
266
266
  visitor
267
267
  end
268
268
 
269
- ###################################################################################################################
269
+ ############################################################################################
270
270
  #
271
271
  # Format the content of tree
272
272
  #
@@ -365,10 +365,14 @@ module TreeRb
365
365
 
366
366
  def prepare_color_map
367
367
  @fileToColor = { }
368
- ENV['LS_COLORS'].split(":").each do |e|
369
- k, v = e.split('=')
370
- @fileToColor[k] = v
368
+
369
+ if ENV['LS_COLORS']
370
+ ENV['LS_COLORS'].split(":").each do |e|
371
+ k, v = e.split('=')
372
+ @fileToColor[k] = v
373
+ end
371
374
  end
375
+
372
376
  end
373
377
 
374
378
  def color_file(filename)
@@ -383,14 +387,11 @@ module TreeRb
383
387
  end
384
388
 
385
389
  def prepare_prefix_map(options)
386
- #
387
390
  # check console character encoding
388
391
  # altre variabili LC_CTYPE
389
392
  # LC_ALL
390
393
  # comando locale
391
394
  # puts "enconding: #{ENV['LANG']}"
392
- #
393
-
394
395
 
395
396
  # │ (ascii 179)
396
397
  # ├ (ascii 195)
@@ -51,6 +51,13 @@ module TreeRb
51
51
  @stack.push(tree_node)
52
52
  end
53
53
 
54
+ #
55
+ # called when the tree node is not accessible or an exception is raise when the node is accessed
56
+ #
57
+ # @param [Object] error
58
+ def cannot_enter_node( tree_node, error)
59
+ end
60
+
54
61
  #
55
62
  # called on tree node at end of the visit i.e. oll subtree are visited
56
63
  #
@@ -92,8 +99,8 @@ module TreeRb
92
99
  # add a block to be called when entering into a tree_node
93
100
  #
94
101
  def on_enter_node(&block)
95
- raise "already defined a delegate" if @delegate
96
- raise "block missing" unless block
102
+ raise 'already defined a delegate' if @delegate
103
+ raise 'block missing' unless block
97
104
  @on_enter_tree_node_blocks << block
98
105
  end
99
106
 
@@ -101,8 +108,8 @@ module TreeRb
101
108
  # add a block to be called when exiting from a TreeNode
102
109
  #
103
110
  def on_exit_node(&block)
104
- raise "already defined a delegate" if @delegate
105
- raise "block missing" unless block
111
+ raise 'already defined a delegate' if @delegate
112
+ raise 'block missing' unless block
106
113
  @on_exit_tree_node_blocks << block
107
114
  end
108
115
 
@@ -110,8 +117,8 @@ module TreeRb
110
117
  # add a block to be called when visiting a leaf node
111
118
  #
112
119
  def on_leaf(&block)
113
- raise "already defined a delegate" if @delegate
114
- raise "block missing" unless block
120
+ raise 'already defined a delegate' if @delegate
121
+ raise 'block missing' unless block
115
122
  @on_visit_leaf_node_blocks << block
116
123
  end
117
124
 
@@ -15,12 +15,12 @@ class Numeric
15
15
 
16
16
  def to_human
17
17
  if self == 0
18
- return "0B"
19
- end
18
+ return '0B'
19
+ end
20
20
  units = %w{B KB MB GB TB}
21
-
21
+
22
22
  e = (Math.log(self)/Math.log(1024)).floor
23
- s = "%.3f" % (to_f / 1024**e)
23
+ s = '%.3f' % (to_f / 1024**e)
24
24
  s.sub(/\.?0*$/, units[e])
25
25
  end
26
26
 
@@ -20,7 +20,7 @@ module TreeRb
20
20
  @dirname = dirname
21
21
  old_dirname = Dir.pwd
22
22
  Dir.chdir( @dirname )
23
- Dir["**/*"].each { |f|
23
+ Dir['**/*'].each { |f|
24
24
  pn = Pathname.new( f ).expand_path
25
25
  # puts "#{self.class.name}#loadfromdir #{f}"
26
26
  next if pn.directory?
@@ -11,7 +11,7 @@ module TreeRb
11
11
  # if dirname is missing, must be supplied when invoking run
12
12
  #
13
13
  # @yield [a, b, c] TreeNodeVisitor
14
- # @yieldparam [optional, types, ...] argname description
14
+ # @yieldparam [optional, types, ...] argument name description
15
15
  # @yieldreturn [optional, types, ...] description
16
16
  #
17
17
  # @overload initialize(dirname)
@@ -112,7 +112,12 @@ module TreeRb
112
112
  # @param [RegEx] pattern
113
113
  #
114
114
  def ignore_dir(pattern)
115
- @ignore_dir_patterns << pattern
115
+ # it transforms all in regexp
116
+ if pattern.kind_of? Regexp
117
+ @ignore_dir_patterns << pattern
118
+ else
119
+ @ignore_dir_patterns << /#{pattern}/
120
+ end
116
121
  self
117
122
  end
118
123
 
@@ -132,7 +137,11 @@ module TreeRb
132
137
  # @param [RegEx] pattern
133
138
  #
134
139
  def match(pattern)
135
- @match_file_patterns << pattern
140
+ if pattern.kind_of? Regexp
141
+ @match_file_patterns << pattern
142
+ else
143
+ @match_file_patterns << /#{pattern}/
144
+ end
136
145
  self
137
146
  end
138
147
 
@@ -167,7 +176,7 @@ module TreeRb
167
176
  end
168
177
 
169
178
  #
170
- # Test common ignore pattern
179
+ # Test match pattern
171
180
  #
172
181
  # @param [String] filename
173
182
  # @return [boolean] if filename match almost one pattern
@@ -237,7 +246,7 @@ module TreeRb
237
246
  end
238
247
 
239
248
  unless @visitor
240
- raise "missing visitor"
249
+ raise 'missing visitor'
241
250
  end
242
251
 
243
252
  #
@@ -250,14 +259,13 @@ module TreeRb
250
259
  private
251
260
 
252
261
  def _include?(patterns, basename)
253
- # return false if the patters.empty?
254
- patterns.find { |pattern|
262
+ patterns.find do |pattern|
255
263
  if pattern.kind_of? Regexp
256
264
  pattern.match(basename)
257
265
  else
258
266
  basename == pattern
259
267
  end
260
- }
268
+ end
261
269
  end
262
270
 
263
271
  #
@@ -284,7 +292,7 @@ module TreeRb
284
292
  @visitor.enter_node(dirname)
285
293
  entries.each do |basename|
286
294
  begin
287
- next if basename == "." or basename == ".." # ignore always "." and ".."
295
+ next if basename == '.' or basename == '..' # ignore always "." and ".."
288
296
  pathname = File.join(dirname, basename)
289
297
 
290
298
  if File.directory?(pathname)
@@ -1,14 +1,14 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'tree_rb/output_dircat/entry'
3
- require 'tree_rb/output_dircat/dircat_visitor'
4
-
5
2
  module TreeRb
6
3
 
7
- class DirCatHelper
4
+ class DirCatOutput
8
5
 
9
6
  def run(directory_tree_walker, options)
7
+ require 'tree_rb/output_dircat/entry'
8
+ require 'tree_rb/output_dircat/dircat_visitor'
9
+
10
10
  unless options[:output]
11
- $stderr.puts "need to specify the -o options"
11
+ $stderr.puts 'need to specify the -o options'
12
12
  else
13
13
  filename = options[:output]
14
14
  visitor = DirCatVisitor.new(filename)
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module DirCat
3
3
 
4
- FORMAT_VERSION="0.2.1"
4
+ FORMAT_VERSION='0.2.1'
5
5
 
6
6
  class EntrySer < OpenStruct
7
7
  end
@@ -1,17 +1,18 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'tree_rb/output_html/directory_to_hash2_visitor'
3
- require 'tree_rb/output_html/erb_render'
4
2
 
5
3
  module TreeRb
6
4
 
7
- class D3jsHelper
5
+ class D3jsOutput
8
6
 
9
7
  def run(directory_tree_walker, dirname, template, output)
8
+ require 'tree_rb/output_plugins/html/directory_to_hash2_visitor'
9
+ require 'tree_rb/output_plugins/html/erb_render'
10
+
10
11
  visitor = DirectoryToHash2Visitor.new(dirname)
11
12
  root = directory_tree_walker.run(visitor).root
12
13
  begin
13
14
  str_json = JSON.pretty_generate(root)
14
- str_json = "var data = " + str_json
15
+ str_json = 'var data = ' + str_json
15
16
 
16
17
  if template
17
18
  render = ErbRender.new(template, str_json)
@@ -5,14 +5,19 @@ module TreeRb
5
5
 
6
6
  def initialize(filename)
7
7
  @db = SQLite3::Database.new(filename)
8
- @db.execute("create table files(path varchar(1024), size integer, digest varchar(40))")
8
+ @db.execute('create table files(path varchar(1024), name varchar(40), size integer, sha1 varchar(40))')
9
9
  end
10
10
 
11
11
  def visit_leaf(pathname)
12
12
  puts pathname
13
13
  stat = File.lstat(pathname)
14
14
  digest = SHA1.file(pathname).hexdigest
15
- @db.execute("insert into files (path, size, digest) values ( \"#{pathname}\", \"#{stat.size}\", \"#{digest}\")")
15
+ sql = 'insert into files (path, name, size, sha1) values ('
16
+ sql << "\"#{File.dirname(pathname)}\","
17
+ sql << "\"#{File.basename(pathname)}\","
18
+ sql << "\"#{stat.size}\","
19
+ sql << "\"#{digest}\")"
20
+ @db.execute(sql)
16
21
 
17
22
  # entry = Entry.from_filename(filename)
18
23
  # me.add_entry(entry)
@@ -32,9 +37,9 @@ module TreeRb
32
37
 
33
38
  def find_duplicates
34
39
  # Loop through digests.
35
- @db.execute("select digest,count(1) as count from files group by digest order by count desc").each do |row|
40
+ @db.execute('select sha1,count(1) as count from files group by sha1 order by count desc').each do |row|
36
41
  if row[1] > 1 # Skip unique files.
37
- puts "Duplicates found:"
42
+ puts 'Duplicates found:'
38
43
  digest = row[0]
39
44
  # List the duplicate files.
40
45
  db.execute("select digest,path from files where digest='#{digest}'").each do |dup_row|
@@ -1,18 +1,20 @@
1
1
  # -*- coding: utf-8 -*-
2
- begin
3
- require 'sqlite3'
4
- require 'tree_rb/output_sqlite/sqlite_dir_tree_visitor'
5
- rescue LoadError
6
- $stderr.puts 'You must gem install sqlite3 to use this output format'
7
- end
8
-
9
2
  module TreeRb
10
3
 
11
- class SqliteHelper
4
+ class SqliteOutput
12
5
 
13
6
  def run(directory_tree_walker, output, options)
7
+
8
+ begin
9
+ require 'sqlite3'
10
+ require 'tree_rb/output_plugins/sqlite/sqlite_dir_tree_visitor'
11
+ rescue LoadError
12
+ $stderr.puts 'You must gem install sqlite3 to use this output format'
13
+ exit(1)
14
+ end
15
+
14
16
  unless options[:output]
15
- $stderr.puts "need to specify the -o options"
17
+ $stderr.puts 'need to specify the -o options'
16
18
  else
17
19
  output.close
18
20
  filename = options[:output]
@@ -1,4 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  module TreeRb
3
- VERSION="0.3.11"
3
+ VERSION='0.3.12'
4
4
  end
@@ -10,9 +10,9 @@ module TreeRb
10
10
  end
11
11
 
12
12
  def enter_node( tree_node )
13
- str = ""
13
+ str = ''
14
14
  (0...@depth).step {
15
- str << " |-"
15
+ str << ' |-'
16
16
  }
17
17
 
18
18
  if @depth == 0
@@ -36,9 +36,9 @@ module TreeRb
36
36
  def visit_leaf( leaf_node )
37
37
  str = ""
38
38
  (0...@depth-1).step {
39
- str << " |-"
39
+ str << ' |-'
40
40
  }
41
- str << " | "
41
+ str << ' | '
42
42
  puts str + leaf_node.name.to_s
43
43
  end
44
44
 
Binary file
@@ -2,7 +2,7 @@
2
2
  #
3
3
  # std lib
4
4
  #
5
- require "stringio"
5
+ require 'stringio'
6
6
  require 'ostruct'
7
7
 
8
8
  #
@@ -13,7 +13,7 @@ require 'tree_rb'
13
13
  require 'tree_rb_cli'
14
14
  include TreeRb
15
15
 
16
- FIXTURES = File.expand_path( File.join( File.dirname(__FILE__), "fixtures" ) )
16
+ FIXTURES = File.expand_path( File.join( File.dirname(__FILE__), 'fixtures') )
17
17
 
18
18
  def capture_output
19
19
  old_stdout, old_stderr = $stdout, $stderr
@@ -1,9 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
3
3
 
4
4
  describe CliJson do
5
5
 
6
- it "should accepts --help switch" do
6
+ it 'should accepts --help switch' do
7
7
  captured = capture_output do
8
8
  args = %w{--help}
9
9
  # args << File.join(FIXTURES, "test_dir_1")
@@ -12,7 +12,7 @@ describe CliJson do
12
12
  captured.out.should match /Usage:/
13
13
  end
14
14
 
15
- it "should accept --version switch" do
15
+ it 'should accept --version switch' do
16
16
  captured = capture_output do
17
17
  args = %w{--version}
18
18
  CliJson.new.parse_args(args)
@@ -21,10 +21,10 @@ describe CliJson do
21
21
  captured.out.should match version
22
22
  end
23
23
 
24
- it "should format simple json file" do
24
+ it 'should format simple json file' do
25
25
  captured = capture_output do
26
26
  args = []
27
- args << File.join(FIXTURES, "test_json", "1.json")
27
+ args << File.join(FIXTURES, 'test_json', '1.json')
28
28
  CliJson.new.parse_args(args)
29
29
  end
30
30
 
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
3
 
4
4
  describe CliTree do
5
5
 
6
- it "should accepts --format html_partition" do
6
+ it 'should accepts --format html_partition' do
7
7
  captured = capture_output do
8
8
  args = %w{--format html_partition}
9
9
  args << File.join(FIXTURES, "test_dir_1")
@@ -12,7 +12,7 @@ describe CliTree do
12
12
  captured.out.should match /d3.layout.partition/
13
13
  end
14
14
 
15
- it "should accepts --format html_tree" do
15
+ it 'should accepts --format html_tree' do
16
16
  captured = capture_output do
17
17
  args = %w{--format html_tree}
18
18
  args << File.join(FIXTURES, "test_dir_1")
@@ -21,7 +21,7 @@ describe CliTree do
21
21
  captured.out.should match /d3.layout.tree/
22
22
  end
23
23
 
24
- it "should accepts --format html_treemap" do
24
+ it 'should accepts --format html_treemap' do
25
25
  captured = capture_output do
26
26
  args = %w{--format html_treemap}
27
27
  args << File.join(FIXTURES, "test_dir_1")
@@ -1,95 +1,126 @@
1
- # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
-
4
- describe CliTree do
5
-
6
- it "should accept --help switch" do
7
- captured = capture_output do
8
- args = %w{--help}
9
- CliTree.new.parse_args(args)
10
- end
11
- captured.out.should match /Usage:/
12
- end
13
-
14
- it "should accept --version switch" do
15
- captured = capture_output do
16
- args = %w{--version}
17
- CliTree.new.parse_args(args)
18
- end
19
- version = TreeRb::VERSION
20
- captured.out.should match version
21
- end
22
-
23
- it "should accepts -d switch (directories only)" do
24
- captured = capture_output do
25
- args = %w{-d}
26
- args << File.join(FIXTURES, "test_dir_1")
27
- CliTree.new.parse_args(args)
28
- end
29
- expected = "test_dir_1\n|-- dir.1\n| `-- dir.1.2\n`-- dir.2\n\n4 directories, 0 files\n"
30
- captured.out.should == expected
31
- captured.out.split("\n").length.should == 6
32
- end
33
-
34
- it "should accepts -da switch (directories only)" do
35
- captured = capture_output do
36
- args = %w{-da}
37
- args << File.join(FIXTURES, "test_dir_1")
38
- CliTree.new.parse_args(args)
39
- end
40
- expected = "test_dir_1\n|-- .dir_with_dot\n|-- dir.1\n| `-- dir.1.2\n`-- dir.2\n\n5 directories, 0 files\n"
41
- captured.out.should == expected
42
- captured.out.split("\n").length.should == 7
43
- end
44
-
45
- it "should accepts -a switch (all files)" do
46
- captured = capture_output do
47
- args = %w{-a}
48
- args << File.join(FIXTURES, "test_dir_1")
49
- CliTree.new.parse_args(args)
50
- end
51
- # pp captured
52
- expected ="test_dir_1\n|-- .dir_with_dot\n| `-- dummy.txt\n|-- dir.1\n| |-- file.1.1\n| `-- dir.1.2\n| `-- file.1.2.1\n`-- dir.2\n `-- file.2.1\n\n5 directories, 4 files\n"
53
- captured.out.should == expected
54
- captured.out.split("\n").length.should == 11
55
- end
56
-
57
- it "should accepts -a switch (all files)" do
58
- captured = capture_output do
59
- args = []
60
- args << File.join(FIXTURES, "test_dir_1")
61
- CliTree.new.parse_args(args)
62
- end
63
- # puts captured
64
- expected ="test_dir_1\n|-- dir.1\n| |-- file.1.1\n| `-- dir.1.2\n| `-- file.1.2.1\n`-- dir.2\n `-- file.2.1\n\n4 directories, 3 files\n"
65
- captured.out.should == expected
66
- captured.out.split("\n").length.should == 9
67
- end
68
-
69
- it "should accepts -A switch (ascii line graphics)" do
70
- captured = capture_output do
71
- args = %w{-A}
72
- args << File.join(FIXTURES, "test_dir_1")
73
- CliTree.new.parse_args(args)
74
- end
75
- # puts captured
76
- expected ="test_dir_1\n├── dir.1\n│ ├── file.1.1\n│ └── dir.1.2\n│ └── file.1.2.1\n└── dir.2\n └── file.2.1\n\n4 directories, 3 files\n"
77
- captured.out.should == expected
78
- end
79
-
80
- it "should show tree with inaccessible directories" do
81
- captured = capture_output do
82
- args = []
83
- args << File.join(FIXTURES, "test_dir_3_with_error")
84
- CliTree.new.parse_args(args)
85
- end
86
- # puts captured
87
-
88
- expected_out="test_dir_3_with_error\n`-- accessible_dir\n\n2 directories, 0 files\n"
89
- expected_err=/Permission denied/
90
- captured.out.should == expected_out
91
- captured.err.should match expected_err
92
- captured.err.should_not be_empty
93
- captured.out.split("\n").length.should == 4
94
- end
95
- end
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
+
4
+ describe CliTree do
5
+
6
+ it "should accept --help switch" do
7
+ captured = capture_output do
8
+ args = %w{--help}
9
+ CliTree.new.parse_args(args)
10
+ end
11
+ captured.out.should match /Usage:/
12
+ end
13
+
14
+ it "should accept --version switch" do
15
+ captured = capture_output do
16
+ args = %w{--version}
17
+ CliTree.new.parse_args(args)
18
+ end
19
+ version = TreeRb::VERSION
20
+ captured.out.should match version
21
+ end
22
+
23
+ it "should accepts -d switch (directories only)" do
24
+ captured = capture_output do
25
+ args = %w{-d}
26
+ args << File.join(FIXTURES, "test_dir_1")
27
+ CliTree.new.parse_args(args)
28
+ end
29
+ expected = "test_dir_1\n|-- dir.1\n| `-- dir.1.2\n`-- dir.2\n\n4 directories, 0 files\n"
30
+ captured.out.should == expected
31
+ captured.out.split("\n").length.should == 6
32
+ end
33
+
34
+ it "should accepts -da switch (directories only)" do
35
+ captured = capture_output do
36
+ args = %w{-da}
37
+ args << File.join(FIXTURES, "test_dir_1")
38
+ CliTree.new.parse_args(args)
39
+ end
40
+ expected = "test_dir_1\n|-- .dir_with_dot\n|-- dir.1\n| `-- dir.1.2\n`-- dir.2\n\n5 directories, 0 files\n"
41
+ captured.out.should == expected
42
+ captured.out.split("\n").length.should == 7
43
+ end
44
+
45
+ it "should accepts -a switch (all files)" do
46
+ captured = capture_output do
47
+ args = %w{-a}
48
+ args << File.join(FIXTURES, "test_dir_1")
49
+ CliTree.new.parse_args(args)
50
+ end
51
+ # pp captured
52
+ expected ="test_dir_1\n|-- .dir_with_dot\n| `-- dummy.txt\n|-- dir.1\n| |-- file.1.1\n| `-- dir.1.2\n| `-- file.1.2.1\n`-- dir.2\n `-- file.2.1\n\n5 directories, 4 files\n"
53
+ captured.out.should == expected
54
+ captured.out.split("\n").length.should == 11
55
+ end
56
+
57
+ it "should accepts -a switch (all files)" do
58
+ captured = capture_output do
59
+ args = []
60
+ args << File.join(FIXTURES, "test_dir_1")
61
+ CliTree.new.parse_args(args)
62
+ end
63
+ # puts captured
64
+ expected ="test_dir_1\n|-- dir.1\n| |-- file.1.1\n| `-- dir.1.2\n| `-- file.1.2.1\n`-- dir.2\n `-- file.2.1\n\n4 directories, 3 files\n"
65
+ captured.out.should == expected
66
+ captured.out.split("\n").length.should == 9
67
+ end
68
+
69
+ it "should accepts -A switch (ascii line graphics)" do
70
+ captured = capture_output do
71
+ args = %w{-A}
72
+ args << File.join(FIXTURES, "test_dir_1")
73
+ CliTree.new.parse_args(args)
74
+ end
75
+ # puts captured
76
+ expected = <<EOS
77
+ test_dir_1
78
+ ├── dir.1
79
+ │ ├── file.1.1
80
+ │ └── dir.1.2
81
+ │ └── file.1.2.1
82
+ └── dir.2
83
+ └── file.2.1
84
+
85
+ 4 directories, 3 files
86
+ EOS
87
+ captured.out.encode('utf-8').should == expected.encode('utf-8')
88
+ end
89
+
90
+ it "should show tree with inaccessible directories" do
91
+
92
+ # begin
93
+ d = File.join(FIXTURES, "test_dir_3_with_error", "no_accessible_dir")
94
+ if File.exists?(d)
95
+ File.chmod(0644,d)
96
+ Dir.rmdir(d)
97
+ end
98
+ FileUtils.mkdir(d)
99
+ File.chmod(0000, d)
100
+ # rescue
101
+ # end
102
+
103
+ captured = capture_output do
104
+ args = []
105
+ args << File.join(FIXTURES, "test_dir_3_with_error")
106
+ CliTree.new.parse_args(args)
107
+ end
108
+ # puts captured
109
+
110
+ expected_out= <<EOS
111
+ test_dir_3_with_error
112
+ `-- accessible_dir
113
+
114
+ 2 directories, 0 files
115
+ EOS
116
+ expected_err=/Permission denied/
117
+
118
+ captured.out.should == expected_out
119
+ captured.err.should match expected_err
120
+ captured.err.should_not be_empty
121
+ captured.out.split("\n").length.should == 4
122
+
123
+ # File.chmod(0644,d)
124
+ # Dir.unlink(d)
125
+ end
126
+ end