milkode 0.2.4 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,130 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/02/20
7
+
8
+ require 'yaml'
9
+ require 'pathname'
10
+ require 'milkode/common/dbdir'
11
+
12
+ module Milkode
13
+ class CdstkYaml
14
+ class YAMLAlreadyExist < RuntimeError
15
+ end
16
+
17
+ class YAMLNotExist < RuntimeError
18
+ end
19
+
20
+ def self.create(path = ".")
21
+ yf = yaml_file(path)
22
+ raise YAMLAlreadyExist.new if FileTest.exist? yf
23
+ obj = CdstkYaml.new(yf, {'contents' => [], 'version' => 0.1})
24
+ obj.save
25
+ return obj
26
+ end
27
+
28
+ def self.load(path = ".")
29
+ yf = yaml_file(path)
30
+ raise YAMLNotExist.new unless FileTest.exist? yf
31
+ open(yf) do |f|
32
+ return CdstkYaml.new(yf, YAML.load(f.read()))
33
+ end
34
+ end
35
+
36
+ def initialize(yaml_file, data)
37
+ @yaml_file = yaml_file
38
+ @data = data
39
+ end
40
+
41
+ def add(dirs)
42
+ contents.concat(dirs.map{|v|{'directory' => v}})
43
+ contents.uniq!
44
+ end
45
+
46
+ def remove(query)
47
+ r = query.select_any?(contents)
48
+ r.each {|v| contents.delete v}
49
+ end
50
+
51
+ def save
52
+ open(@yaml_file, "w") { |f| YAML.dump(@data, f) }
53
+ end
54
+
55
+ def contents
56
+ @data['contents']
57
+ end
58
+
59
+ def package_num
60
+ @data['contents'].size
61
+ end
62
+
63
+ def directorys
64
+ contents.map{|v|v['directory']}
65
+ end
66
+
67
+ def version
68
+ @data['version']
69
+ end
70
+
71
+ def list(query = nil)
72
+ query ? query.select_all?(contents) : contents
73
+ end
74
+
75
+ def exist?(shortname)
76
+ @data['contents'].find {|v| File.basename(v['directory']) == shortname }
77
+ end
78
+
79
+ def cant_add_directory?(dir)
80
+ @data['contents'].find {|v|
81
+ v['directory'] != File.expand_path(dir) &&
82
+ File.basename(v['directory']) == File.basename(dir)
83
+ }
84
+ end
85
+
86
+ def cleanup
87
+ contents.delete_if do |v|
88
+ if (!File.exist? v['directory'])
89
+ yield v if block_given?
90
+ true
91
+ else
92
+ false
93
+ end
94
+ end
95
+ end
96
+
97
+ def package_root(dir)
98
+ @data['contents'].find do |v|
99
+ v if dir =~ /^#{v['directory']}/
100
+ end
101
+ end
102
+
103
+ def package_root_dir(dir)
104
+ package = package_root(dir)
105
+ (package) ? package['directory'] : nil
106
+ end
107
+
108
+ def self.yaml_file(path)
109
+ Dbdir.yaml_path(path)
110
+ end
111
+
112
+ class Query
113
+ def initialize(keywords)
114
+ @keywords = keywords
115
+ end
116
+
117
+ def select_any?(contents)
118
+ contents.find_all do |v|
119
+ @keywords.any? {|s| File.basename(v['directory']).include? s }
120
+ end
121
+ end
122
+
123
+ def select_all?(contents)
124
+ contents.find_all do |v|
125
+ @keywords.all? {|s| File.basename(v['directory']).include? s }
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/03/11
7
+
8
+ require 'test/unit'
9
+ Test::Unit::AutoRunner.run(true, '.')
10
+
11
+
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/02/20
7
+
8
+ require 'milkode/common/dir'
9
+ require 'test/unit'
10
+ require 'file_test_utils'
11
+
12
+ class TestDir < Test::Unit::TestCase
13
+ include FileTestUtils
14
+
15
+ def test_emptydir?
16
+ assert_equal true, Dir.emptydir?(".")
17
+ assert_equal false, Dir.emptydir?("..")
18
+ end
19
+ end
20
+
21
+
@@ -0,0 +1,78 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief Milkodeテスト用のワーク領域確保
5
+ # @author ongaeshi
6
+ # @date 2011/12/14
7
+
8
+ require 'test_helper'
9
+ require 'rubygems'
10
+ require 'groonga'
11
+ require 'pathname'
12
+ require 'fileutils'
13
+ require 'milkode/cdstk/cdstk'
14
+ require 'milkode/common/dbdir'
15
+
16
+ class MilkodeTestWork
17
+ def initialize(option = nil)
18
+ @option = option
19
+
20
+ create_tmp_dir
21
+
22
+ if (@option[:default_db])
23
+ @old_path = Dbdir.milkode_db_dir
24
+ path = expand_path(".milkode_db_dir")
25
+ Dbdir.set_milkode_db_dir path
26
+ open(Dbdir.milkode_db_dir, "w") {|f| f.print expand_path("db1") }
27
+ end
28
+
29
+ init_db("db1")
30
+ end
31
+
32
+ def init_db(name)
33
+ dbdir = expand_path(name)
34
+ FileUtils.mkdir_p dbdir
35
+ Dir.chdir(dbdir) { cdstk.init }
36
+ end
37
+
38
+ def add_package(name, package_path)
39
+ dbdir = expand_path(name)
40
+
41
+ Dir.chdir(dbdir) do
42
+ cdstk.add [package_path]
43
+ end
44
+ end
45
+
46
+ def teardown
47
+ FileUtils.rm_rf(@tmp_dir.to_s)
48
+ Dbdir.set_milkode_db_dir @old_path if (@option[:default_db])
49
+ end
50
+
51
+ def path(path)
52
+ File.join @tmp_dir.to_s, path
53
+ end
54
+
55
+ def expand_path(path)
56
+ File.expand_path path(path)
57
+ end
58
+
59
+ def pwd
60
+ cdstk.pwd({})
61
+ end
62
+
63
+ private
64
+
65
+ def create_tmp_dir
66
+ @tmp_dir = Pathname(File.dirname(__FILE__)) + "milkode_test_work"
67
+ FileUtils.rm_rf(@tmp_dir.to_s)
68
+ FileUtils.mkdir_p(@tmp_dir.to_s)
69
+ end
70
+
71
+ def cdstk
72
+ Cdstk.new(StringIO.new, Dbdir.select_dbdir)
73
+ end
74
+
75
+ end
76
+
77
+
78
+
@@ -50,10 +50,20 @@ class TestCdstk < Test::Unit::TestCase
50
50
  io.puts('--- add notfound ---')
51
51
  obj.add(['notfound.html'])
52
52
 
53
- io.puts('--- update ---')
53
+ io.puts('--- update_all ---')
54
54
  FileUtils.touch('packages/zip/abc/c.txt')
55
55
  FileUtils.touch('packages/zip/abc/d.txt')
56
- obj.update
56
+ obj.update_all
57
+
58
+ io.puts('--- update --all ---')
59
+ FileUtils.touch('packages/zip/abc/e.txt')
60
+ obj.update([], {:all => true})
61
+
62
+ io.puts('--- update in package dir ---')
63
+ Dir.chdir('packages/zip/abc') do
64
+ # setdbコマンドが無いので上手く動かせない
65
+ # obj.update([], {})
66
+ end
57
67
 
58
68
  io.puts('--- remove ---')
59
69
  obj.remove(['findgrep'], {:force => true})
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/02/20
7
+
8
+ require 'milkode_test_work'
9
+ require 'milkode/cdstk/cdstk_command'
10
+
11
+ class TestCdstkCommand < Test::Unit::TestCase
12
+ def setup
13
+ @work = MilkodeTestWork.new({:default_db => true})
14
+ end
15
+
16
+ def test_main
17
+ t_setdb_set
18
+ t_setdb_reset
19
+ end
20
+
21
+ def teardown
22
+ @work.teardown
23
+ end
24
+
25
+ private
26
+
27
+ def t_setdb_set
28
+ # デフォルトデータベースの切り替え
29
+ CdstkCommand.setdb_set(@work.expand_path "db1")
30
+
31
+ # デフォルトデータベースの切り替え
32
+ # @work.init_db( "db2" )
33
+ # CdstkCommand.setdb_set(@work.expand_path "db2")
34
+
35
+ # 存在していないデータベースを指定するとエラー
36
+ assert_raise(Milkode::CdstkCommand::NotExistDatabase) { CdstkCommand.setdb_set("not_found") }
37
+ end
38
+
39
+ def t_setdb_reset
40
+ path = @work.expand_path '.milkode_db_dir'
41
+
42
+ CdstkCommand.setdb_set(@work.expand_path "db1")
43
+ assert File.exist?(path)
44
+
45
+ CdstkCommand.setdb_reset
46
+ assert !File.exist?(path)
47
+ end
48
+ end
49
+
50
+
@@ -73,14 +73,14 @@ class TestCdstkYaml < Test::Unit::TestCase
73
73
  contents = [{d => 'key'}, {d => 'keyword'}, {d => 'not'}]
74
74
 
75
75
  query = CdstkYaml::Query.new(['key'])
76
- assert_equal [{d => 'key'}, {d => 'keyword'}], query.select(contents)
76
+ assert_equal [{d => 'key'}, {d => 'keyword'}], query.select_any?(contents)
77
77
 
78
78
  query = CdstkYaml::Query.new(['word'])
79
- assert_equal [{d => 'keyword'}], query.select(contents)
79
+ assert_equal [{d => 'keyword'}], query.select_any?(contents)
80
80
 
81
81
  contents = [{d => 'a/dir'}, {d => 'b/dia'}]
82
82
  query = CdstkYaml::Query.new(['a'])
83
- assert_equal [{d => 'b/dia'}], query.select(contents) # ディレクトリ名は含めない
83
+ assert_equal [{d => 'b/dia'}], query.select_any?(contents) # ディレクトリ名は含めない
84
84
  end
85
85
 
86
86
  def test_list
@@ -142,6 +142,23 @@ EOF
142
142
  assert_nil yaml.exist?('dir123')
143
143
  assert_nil yaml.exist?('dir')
144
144
  end
145
+
146
+ def test_package_root
147
+ src = <<EOF
148
+ version: 0.1
149
+ contents:
150
+ - directory: /a/dir1
151
+ - directory: /path/to/dir
152
+ - directory: /a/b/c
153
+ EOF
154
+
155
+ yaml = CdstkYaml.new('dummy.yaml', YAML.load(src))
156
+
157
+ assert_equal nil , yaml.package_root_dir('/not_dir')
158
+ assert_equal "/a/dir1" , yaml.package_root_dir('/a/dir1/dir3')
159
+ assert_equal nil , yaml.package_root_dir('/hoge/a/dir1/dir3')
160
+ assert_equal '/path/to/dir', yaml.package_root_dir('/path/to/dir')
161
+ end
145
162
 
146
163
  def teardown
147
164
  FileUtils.cd(@prev_dir)
@@ -0,0 +1,71 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/12/03
7
+
8
+ require 'milkode/cdstk/cli_cdstk.rb'
9
+ require 'milkode/common/dbdir'
10
+ require 'test_helper'
11
+ require 'milkode/cdstk/cdstk_command'
12
+ require 'milkode_test_work'
13
+
14
+ class TestCLI_Cdstk < Test::Unit::TestCase
15
+ def setup
16
+ @first_default_dir = Dbdir.default_dir
17
+ @work = MilkodeTestWork.new({:default_db => true})
18
+ @work.add_package "db1", @work.expand_path("../data/a_project")
19
+ end
20
+
21
+ def test_main
22
+ t_grep
23
+ t_mcd
24
+ t_setdb
25
+ end
26
+
27
+ def teardown
28
+ @work.teardown
29
+ end
30
+
31
+ private
32
+
33
+ def t_grep
34
+ command("grep")
35
+ command("grep not_found")
36
+ command("grep require -a")
37
+ end
38
+
39
+ def t_mcd
40
+ assert_match /mcd/, command("mcd")
41
+ end
42
+
43
+ def t_setdb
44
+ # 引数無しで現在の値を表示
45
+ assert_equal @work.expand_path("db1") + "\n", command("setdb")
46
+
47
+ # .milkode_db_dir を書き換えてテスト
48
+ open(@work.path(".milkode_db_dir"), "w") {|f| f.print "/a/custom/db" }
49
+ assert_equal "/a/custom/db\n", command("setdb")
50
+
51
+ # データベースではないディレクトリに切り替ようとするとエラー
52
+ assert_match(/fatal:/, command("setdb /a/write/test"))
53
+
54
+ # 切り替え
55
+ @work.init_db("db2")
56
+ assert_match "Set default db", command("setdb #{@work.path "db2"}")
57
+
58
+ # リセット
59
+ assert_not_equal @first_default_dir, Dbdir.default_dir
60
+ command("setdb --reset")
61
+ assert_equal @first_default_dir, Dbdir.default_dir
62
+ end
63
+
64
+ private
65
+
66
+ def command(arg)
67
+ io = StringIO.new
68
+ CLI_Cdstk.execute(io, arg.split)
69
+ io.string
70
+ end
71
+ end
@@ -0,0 +1,79 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/12/03
7
+
8
+ require 'milkode/grep/cli_grep.rb'
9
+ require 'test_helper'
10
+ require 'milkode_test_work'
11
+
12
+ class TestCLI_Grep < Test::Unit::TestCase
13
+ def setup
14
+ @work = MilkodeTestWork.new({:default_db => true})
15
+ @work.add_package "db1", @work.expand_path("../data/a_project")
16
+ @work.add_package "db1", @work.expand_path("../data/b_project")
17
+ end
18
+
19
+ def test_main
20
+ # 全てを test_* にすると、毎回setup, teardown が走ってデータベースを生成する時間がもったいないので
21
+ t_basic
22
+ t_not_found_package
23
+ t_not_package_root
24
+ t_exec_onlysuffix
25
+ t_cache
26
+ end
27
+
28
+ def teardown
29
+ @work.teardown
30
+ end
31
+
32
+ private
33
+
34
+ def t_basic
35
+ io = StringIO.new
36
+
37
+ CLI_Grep.execute(io, "".split)
38
+ CLI_Grep.execute(io, "-a test".split)
39
+ CLI_Grep.execute(io, "-p a_project test".split)
40
+
41
+ # puts io.string
42
+ end
43
+
44
+ def t_not_found_package
45
+ io = StringIO.new
46
+ CLI_Grep.execute(io, "-p c_project test".split)
47
+ assert_match "fatal:", io.string
48
+ end
49
+
50
+ def t_not_package_root
51
+ Dir.chdir(File.join(File.dirname(__FILE__))) do
52
+ io = StringIO.new
53
+ CLI_Grep.execute(io, "require".split)
54
+ assert_match "fatal:", io.string
55
+ end
56
+
57
+ Dir.chdir(File.join(File.dirname(__FILE__), "data/a_project")) do
58
+ io = StringIO.new
59
+ CLI_Grep.execute(io, "require".split)
60
+ assert_no_match /fatal:/, io.string
61
+ end
62
+ end
63
+
64
+ def t_exec_onlysuffix
65
+ io = StringIO.new
66
+ CLI_Grep.execute(io, "-p a_project -s rb".split)
67
+ assert_match "a_project/cdstk.rb\n", io.string
68
+ end
69
+
70
+ def t_cache
71
+ io = StringIO.new
72
+ CLI_Grep.execute(io, "-p a_project test --cache".split)
73
+ assert_match "grenfiletest", io.string
74
+ end
75
+ end
76
+
77
+
78
+
79
+