tree.rb 0.3.11 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -1,22 +1,38 @@
1
- # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
-
4
- describe CliTree do
5
-
6
- it "should accepts '--format sqlite -o test.db' switches" do
7
- db_filename = File.join(FIXTURES, 'tmp', 'test.db')
8
-
9
- File.unlink(db_filename) if File.exist?(db_filename)
10
-
11
- captured = capture_output do
12
- args = %w{--format sqlite -o } << db_filename
13
- args << File.join(FIXTURES, "test_dir_1")
14
- CliTree.new.parse_args(args)
15
- end
16
-
17
- File.exist?(db_filename).should be_true
18
-
19
- File.unlink(db_filename) if File.exist?(db_filename)
20
- end
21
-
22
- end
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
3
+
4
+ describe CliTree do
5
+
6
+
7
+ before(:each) do
8
+ @db_filename = File.join(FIXTURES, 'tmp', 'test.db')
9
+ if File.exist?(@db_filename)
10
+ File.chmod(0644, @db_filename)
11
+ File.unlink(@db_filename)
12
+ end
13
+ end
14
+
15
+ after(:each) do
16
+ if File.exist?(@db_filename)
17
+ File.chmod(0644, @db_filename)
18
+ File.unlink(@db_filename)
19
+ end
20
+ end
21
+
22
+ it "should accepts '--format sqlite -o test.db' switches" do
23
+
24
+ captured = capture_output do
25
+ args = %w{--format sqlite -o } << @db_filename
26
+ args << File.join(FIXTURES, 'test_dir_1')
27
+ CliTree.new.parse_args(args)
28
+ end
29
+ File.exist?(@db_filename).should be_true
30
+
31
+ db = SQLite3::Database.new(@db_filename)
32
+ ar = db.execute('select count(*) from files')
33
+ nr_files = ar[0][0]
34
+ nr_files.should == 3
35
+ db.close
36
+ end
37
+
38
+ end
@@ -1,135 +1,135 @@
1
- # -*- coding: utf-8 -*-
2
- require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
-
4
- describe TreeNode do
5
-
6
- it "should initialize correctly" do
7
- ta = TreeNode.new("a")
8
- ta.should be_root
9
-
10
- ln1 = LeafNode.new("1", ta)
11
- ln1.parent.should == ta
12
- ln1.should_not be_root
13
-
14
- ln2 = LeafNode.new("2", ta)
15
- tb = TreeNode.new("b", ta)
16
- ln3 = LeafNode.new("3", tb)
17
- ln3.parent.should == tb
18
-
19
- # test depth
20
- tb.depth.should == 2
21
- ln3.depth.should == 3
22
-
23
- # test nr_nodes
24
- tb.nr_nodes.should == 1
25
- ta.nr_nodes.should == 4
26
-
27
- # puts ta.to_str
28
- end
29
-
30
- it "should add child and leaf" do
31
- ta = TreeNode.new("a")
32
- ta.should be_root
33
-
34
- ln1 = LeafNode.new("1")
35
- ta.add_leaf(ln1)
36
- ln1.parent.should == ta
37
-
38
- ln2 = LeafNode.new("2", ta)
39
- ta.add_leaf(ln2)
40
-
41
- tb = TreeNode.new("b", ta)
42
- ln3 = LeafNode.new("3", tb)
43
- tb.add_leaf(ln3)
44
- ln3.parent.should == tb
45
-
46
- ta.add_child(tb)
47
- # puts ta.to_str
48
- end
49
-
50
- it "next and prev" do
51
- ta = TreeNode.new("a")
52
- ta.prev.should be_nil
53
- ta.next.should be_nil
54
-
55
- ln1 = LeafNode.new("1", ta)
56
- ln1.prev.should be_nil
57
- ln1.next.should be_nil
58
-
59
- ln2 = LeafNode.new("2", ta)
60
- ln2.prev.should == ln1
61
- ln2.next.should be_nil
62
-
63
- ln3 = LeafNode.new("3", ta)
64
- ln2.next.should == ln3
65
-
66
- tb = TreeNode.new("b", ta)
67
- tb.next.should be_nil
68
- tb.prev.should be_nil
69
-
70
- tc = TreeNode.new("c", ta)
71
- tc.prev.should == tb
72
- tc.next.should be_nil
73
- end
74
-
75
- context "navigate tree" do
76
-
77
- before do
78
- @tree = TreeNode.new("a")
79
- ln1 = LeafNode.new("1", @tree)
80
- ln2 = LeafNode.new("2", @tree)
81
- @sub_tree = TreeNode.new("b", @tree)
82
- @ln3 = LeafNode.new("3", @sub_tree)
83
- @ln4 = LeafNode.new("12", @sub_tree)
84
- end
85
-
86
- it "nr_nodes and nr_leaves and nr_children" do
87
- @tree.nr_nodes.should == 5
88
- @tree.nr_leaves.should == 4
89
- @tree.nr_children.should == 1
90
-
91
- @sub_tree.nr_nodes.should == 2
92
- @sub_tree.nr_leaves.should == 2
93
- @sub_tree.nr_children.should == 0
94
- end
95
-
96
-
97
- context "find" do
98
-
99
- it "find by string" do
100
- @tree.find("a").should === @tree
101
- @tree.find("b").should === @sub_tree
102
- @tree.find("3").should === @ln3
103
- @tree.find("not existent").should be_nil
104
- end
105
-
106
- it "find by regex" do
107
- @tree.find(/[a,b]/).should === @tree
108
- @tree.find(/[b,c]/).should === @sub_tree
109
- @tree.find(/\d\d/).should === @ln4
110
- @tree.find(/not existent/).should be_nil
111
- end
112
-
113
- it "find with block" do
114
- @tree.find { |e| e.content == "a" }.should === @tree
115
- @tree.find { |e| e.content == "b" }.should === @sub_tree
116
- @tree.find { |e| e.content == "3" }.should === @ln3
117
- @tree.find { |e| e.content == "not existent" }.should be_nil
118
- end
119
-
120
- it "to_str" do
121
- out = <<EOS
122
- a
123
- |-- 1
124
- |-- 2
125
- `-- b
126
- |-- 3
127
- `-- 12
128
- EOS
129
- @tree.to_str.should == out
130
- end
131
-
132
- end
133
-
134
- end
135
- end
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), "..", "..", "spec_helper")
3
+
4
+ describe TreeNode do
5
+
6
+ it "should initialize correctly" do
7
+ ta = TreeNode.new("a")
8
+ ta.should be_root
9
+
10
+ ln1 = LeafNode.new("1", ta)
11
+ ln1.parent.should == ta
12
+ ln1.should_not be_root
13
+
14
+ ln2 = LeafNode.new("2", ta)
15
+ tb = TreeNode.new("b", ta)
16
+ ln3 = LeafNode.new("3", tb)
17
+ ln3.parent.should == tb
18
+
19
+ # test depth
20
+ tb.depth.should == 2
21
+ ln3.depth.should == 3
22
+
23
+ # test nr_nodes
24
+ tb.nr_nodes.should == 1
25
+ ta.nr_nodes.should == 4
26
+
27
+ # puts ta.to_str
28
+ end
29
+
30
+ it "should add child and leaf" do
31
+ ta = TreeNode.new("a")
32
+ ta.should be_root
33
+
34
+ ln1 = LeafNode.new("1")
35
+ ta.add_leaf(ln1)
36
+ ln1.parent.should == ta
37
+
38
+ ln2 = LeafNode.new("2", ta)
39
+ ta.add_leaf(ln2)
40
+
41
+ tb = TreeNode.new("b", ta)
42
+ ln3 = LeafNode.new("3", tb)
43
+ tb.add_leaf(ln3)
44
+ ln3.parent.should == tb
45
+
46
+ ta.add_child(tb)
47
+ # puts ta.to_str
48
+ end
49
+
50
+ it "next and prev" do
51
+ ta = TreeNode.new("a")
52
+ ta.prev.should be_nil
53
+ ta.next.should be_nil
54
+
55
+ ln1 = LeafNode.new("1", ta)
56
+ ln1.prev.should be_nil
57
+ ln1.next.should be_nil
58
+
59
+ ln2 = LeafNode.new("2", ta)
60
+ ln2.prev.should == ln1
61
+ ln2.next.should be_nil
62
+
63
+ ln3 = LeafNode.new("3", ta)
64
+ ln2.next.should == ln3
65
+
66
+ tb = TreeNode.new("b", ta)
67
+ tb.next.should be_nil
68
+ tb.prev.should be_nil
69
+
70
+ tc = TreeNode.new("c", ta)
71
+ tc.prev.should == tb
72
+ tc.next.should be_nil
73
+ end
74
+
75
+ context "navigate tree" do
76
+
77
+ before do
78
+ @tree = TreeNode.new("a")
79
+ ln1 = LeafNode.new("1", @tree)
80
+ ln2 = LeafNode.new("2", @tree)
81
+ @sub_tree = TreeNode.new("b", @tree)
82
+ @ln3 = LeafNode.new("3", @sub_tree)
83
+ @ln4 = LeafNode.new("12", @sub_tree)
84
+ end
85
+
86
+ it "nr_nodes and nr_leaves and nr_children" do
87
+ @tree.nr_nodes.should == 5
88
+ @tree.nr_leaves.should == 4
89
+ @tree.nr_children.should == 1
90
+
91
+ @sub_tree.nr_nodes.should == 2
92
+ @sub_tree.nr_leaves.should == 2
93
+ @sub_tree.nr_children.should == 0
94
+ end
95
+
96
+
97
+ context "find" do
98
+
99
+ it "find by string" do
100
+ @tree.find("a").should === @tree
101
+ @tree.find("b").should === @sub_tree
102
+ @tree.find("3").should === @ln3
103
+ @tree.find("not existent").should be_nil
104
+ end
105
+
106
+ it "find by regex" do
107
+ @tree.find(/[a,b]/).should === @tree
108
+ @tree.find(/[b,c]/).should === @sub_tree
109
+ @tree.find(/\d\d/).should === @ln4
110
+ @tree.find(/not existent/).should be_nil
111
+ end
112
+
113
+ it "find with block" do
114
+ @tree.find { |e| e.content == "a" }.should === @tree
115
+ @tree.find { |e| e.content == "b" }.should === @sub_tree
116
+ @tree.find { |e| e.content == "3" }.should === @ln3
117
+ @tree.find { |e| e.content == "not existent" }.should be_nil
118
+ end
119
+
120
+ it "to_str" do
121
+ out = <<EOS
122
+ a
123
+ |-- 1
124
+ |-- 2
125
+ `-- b
126
+ |-- 3
127
+ `-- 12
128
+ EOS
129
+ @tree.to_str.should == out
130
+ end
131
+
132
+ end
133
+
134
+ end
135
+ end
@@ -1,15 +1,15 @@
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 DirProcessor do
5
5
 
6
- it "should capture all files" do
6
+ it 'should capture all files' do
7
7
  files = []
8
8
  dp = DirProcessor.new { |f| files << f }
9
9
  dp.process(FIXTURES)
10
10
 
11
11
  puts files
12
- files.length.should == 6
12
+ files.length.should == 5
13
13
  end
14
14
 
15
15
  end
@@ -0,0 +1,59 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
3
+
4
+ describe DirTreeWalker do
5
+
6
+ it 'should accept option :ignore with regex' do
7
+ walker = DirTreeWalker.new :ignore => /^\./
8
+ walker.ignore_file?('.thumbnails').should be_true
9
+ walker.ignore_dir?('.thumbnails').should be_true
10
+ end
11
+
12
+ it 'should accept option :ignore with string' do
13
+ walker = DirTreeWalker.new :ignore => '.git'
14
+ walker.ignore_file?('.git').should be_true
15
+ walker.ignore_dir?('.git').should be_true
16
+ end
17
+
18
+ it 'should accept option :ignore_dir' do
19
+ dtw = DirTreeWalker.new :ignore_dir => [/^\./, "private_dir" ]
20
+ dtw.should be_ignore_dir '.git'
21
+ dtw.should be_ignore_dir 'private_dir'
22
+ end
23
+
24
+ it 'should accept option :ignore_file' do
25
+ dtw = DirTreeWalker.new :ignore_file => [/.xml/, /(ignore)|(orig)/ ]
26
+ dtw.should be_ignore_file 'pippo.xml'
27
+ end
28
+
29
+ it 'should accept option :match with string' do
30
+ dtw = DirTreeWalker.new :match => '.jpg'
31
+ dtw.should be_match 'foo.jpg'
32
+ end
33
+
34
+ it 'should accept option :match with regex' do
35
+ dtw = DirTreeWalker.new :match => /.jpg/
36
+ dtw.should be_match 'foo.jpg'
37
+ end
38
+
39
+ it 'should ignore files and directory' do
40
+ walker = DirTreeWalker.new('.')
41
+
42
+ walker.ignore(/^\./)
43
+ walker.ignore_file?('.thumbnails').should be_true
44
+ walker.ignore_dir?('.thumbnails').should be_true
45
+
46
+ walker.ignore_dir('thumbnails')
47
+ walker.ignore_dir?('.thumbnails').should be_true
48
+ walker.ignore_dir?('thumbnails').should be_true
49
+ walker.ignore_dir?('pippo').should be_false
50
+
51
+ walker.ignore_file('xvpics')
52
+ walker.ignore_file?('xvpics').should be_true
53
+
54
+ walker.ignore('sub')
55
+ walker.ignore_file?('[Dsube]').should be_false
56
+ walker.ignore_dir?('[Dsube]').should be_false
57
+ end
58
+
59
+ end
@@ -0,0 +1,62 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
3
+
4
+ describe DirTreeWalker do
5
+
6
+ it 'should raise exception when run on inexistent directory' do
7
+
8
+ dtw = DirTreeWalker.new(match: /.jpg/)
9
+ root_dir = 'C:\GioProg\interzone\Dropbox\foto.altro\wallpapers\1920x1200_dell_16x10\color'
10
+ dtw.run root_dir do
11
+ on_leaf do |pathname|
12
+ puts "- #{pathname}"
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ it 'should accumulate file names' do
19
+ dir_tree_walker = DirTreeWalker.new(File.join(FIXTURES, "test_dir_1"))
20
+
21
+ accumulator = []
22
+ visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename(pathname) }
23
+ dir_tree_walker.run(visitor)
24
+ accumulator.length.should == 9
25
+ accumulator.sort.should == %w{ test_dir_1 dir.1 dir.1.2 file.1.2.1 file.1.1 dir.2 file.2.1 .dir_with_dot dummy.txt }.sort
26
+ end
27
+
28
+ it 'should accumulate file names 2' do
29
+ dir_tree_walker = DirTreeWalker.new(File.join(FIXTURES, "test_dir_2"))
30
+ dir_tree_walker.ignore('sub')
31
+
32
+ accumulator = []
33
+ visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename(pathname) }
34
+ dir_tree_walker.run(visitor)
35
+ accumulator.length.should == 2
36
+ accumulator.sort.should == %w{ [Dsube] test_dir_2 }.sort
37
+ end
38
+
39
+ it 'should ignore not accessible directory' do
40
+
41
+ dir = File.join(FIXTURES, 'test_dir_3_with_error')
42
+
43
+ f1 = File.join(dir, 'no_accessible_dir')
44
+ Dir.rmdir(f1) if File.exist?(f1)
45
+ Dir.mkdir(f1, 0000)
46
+
47
+ f2 = File.join(dir, "accessible_dir")
48
+ Dir.rmdir(f2) if File.exist?(f2)
49
+ Dir.mkdir(f2)
50
+
51
+ dir_tree_walker = DirTreeWalker.new(File.join(FIXTURES, "test_dir_3_with_error"))
52
+ accumulator = []
53
+ visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename(pathname) }
54
+ dir_tree_walker.run(visitor)
55
+ accumulator.length.should == 2
56
+ accumulator.sort.should == %w{accessible_dir test_dir_3_with_error }.sort
57
+
58
+ # Dir.rmdir(f1)
59
+ # Dir.rmdir(f2)
60
+ end
61
+
62
+ end