cotta 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ require 'spec'
2
+ dir = File.join(File.dirname(__FILE__))
3
+ require dir + '/cotta_file_behaviors'
4
+ require dir + '/../test'
5
+
6
+ module Cotta
7
+ describe InMemorySystem, 'with cotta file' do
8
+ it_should_behave_like 'CottaFileBehaviors'
9
+
10
+ def create_system
11
+ @system = Cotta.in_memory
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec'
2
+ dir = File.dirname(__FILE__)
3
+ require dir + '/../test'
4
+ require dir + '/cotta_file_behaviors'
5
+ require dir + '/physical_system_stub'
6
+
7
+ module Cotta
8
+ describe PhysicalSystem, 'with cotta file' do
9
+ it_should_behave_like 'CottaFileBehaviors'
10
+
11
+ def create_system
12
+ @system = Cotta.factory(PhysicalSystemStub.new)
13
+ end
14
+
15
+ before do
16
+ create_system unless @system
17
+ end
18
+
19
+ it 'copying binary files properly' do
20
+ logo_gif = FileFactory.parent_dir(__FILE__).file('logo.gif')
21
+ content = logo_gif.read_binary {|io| io.read}
22
+ target = CottaFile.new(@system, Pathname.new('dir/logo.gif'))
23
+ target.parent.mkdirs
24
+ target.write_binary do |io|
25
+ io.write content
26
+ end
27
+ expected_stat = logo_gif.stat
28
+ actual_stat = target.stat
29
+ actual_stat.size.should == expected_stat.size
30
+ end
31
+
32
+ it 'zip and unzip' do
33
+ logo_gif = FileFactory.parent_dir(__FILE__).file('logo.gif')
34
+ content = logo_gif.read_binary {|io| io.read}
35
+ dir = CottaDir.new(@system, Pathname.new('dir'))
36
+ target_dir = dir.dir('target')
37
+ target_dir.file(logo_gif.name).write_binary {|io| io.write content}
38
+ zip_file = target_dir.archive.zip
39
+ extract_dir = dir.dir('extract')
40
+ file_to_unzip = zip_file.copy_to(extract_dir.file(zip_file.name))
41
+ extracted_dir = file_to_unzip.unzip.extract
42
+
43
+ extracted_dir.file('logo.gif').stat.size.should == logo_gif.stat.size
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec'
2
+ require File.dirname(__FILE__) + '/../test'
3
+
4
+ module Cotta
5
+ describe FileFactory, 'zip support' do
6
+ before do
7
+ @cotta = FileFactory.new(InMemorySystem.new)
8
+ end
9
+
10
+ it 'extract from a tar file' do
11
+ tar_file = FileFactory.parent_dir(__FILE__).file('tar_test.tar')
12
+ dir = @cotta.dir('dir/extract')
13
+ tar_file.extract(dir)
14
+ dir.list.size.should == 3
15
+ dir.file('one.txt').load.should == 'one'
16
+ dir.file('two.txt').load.should == 'two'
17
+ dir.file('three.txt').load.should == 'three'
18
+ end
19
+
20
+ it 'should archive files in the directory to a file' do
21
+ source = @cotta.dir('dir/source')
22
+ source.file('one.txt').save('one')
23
+ source.file('two.txt').save('two')
24
+ source.file('three.txt').save('three')
25
+ tar_file = @cotta.file('target.tar')
26
+ source.archive(tar_file)
27
+
28
+ target_dir = @cotta.dir('target')
29
+ tar_file.extract(target_dir)
30
+ target_dir.file('one.txt').load.should == 'one'
31
+ target_dir.file('two.txt').load.should == 'two'
32
+ target_dir.file('three.txt').load.should == 'three'
33
+ target_dir.should have(3).list
34
+ end
35
+
36
+ it 'archive subdirectories' do
37
+ source = @cotta.dir('dir/source')
38
+ sub = source.dir('sub')
39
+ sub.file('one.txt').save('one')
40
+ sub.file('two.txt').save('two')
41
+ tar_file = @cotta.file('target.tar')
42
+ source.archive(tar_file)
43
+
44
+ target_dir = @cotta.dir('target')
45
+ tar_file.extract(target_dir)
46
+ target_dir.should have(1).list
47
+ target_dir.dir('sub').should be_exists
48
+ target_dir.dir('sub').should have(2).list
49
+ target_dir.file('sub/one.txt').load.should == 'one'
50
+ target_dir.file('sub/two.txt').load.should == 'two'
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec'
2
+
3
+ require File.dirname(__FILE__) + '/../test'
4
+
5
+ describe Cotta do
6
+ it 'should run example' do
7
+ #system implementation is injected here
8
+ cotta = Cotta.in_memory
9
+ file = cotta.file('dir/file.txt')
10
+ file.should_not be_exists
11
+ # parent directories are created automatically
12
+ file.save('my content')
13
+ file2 = cotta.file('dir/file2.txt')
14
+ file2.should_not be_exists
15
+ file.copy_to(file2)
16
+ file2.should be_exists
17
+ file2.load.should == 'my content'
18
+ file2.read {|file| puts file.gets}
19
+ end
20
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec'
2
+
3
+ require File.dirname(__FILE__) + '/../test'
4
+
5
+ describe Cotta do
6
+ before do
7
+ # Given
8
+ @system = Cotta::InMemorySystem.new
9
+ @cotta = Cotta::FileFactory.new(@system)
10
+ end
11
+
12
+ =begin not for release 1.0
13
+ it 'shell out command to system' do
14
+ # Given
15
+ # When
16
+ @system.output_for_command('shell command', 'test')
17
+ @cotta.shell('shell command')
18
+ # Ensure
19
+ @system.executed_commands.length.should== 1
20
+ @system.executed_commands[0].should == 'shell command'
21
+ end
22
+ =end
23
+
24
+ it 'instantiate dir from cotta' do
25
+ dir = @cotta.dir('dirname')
26
+ dir.name.should == 'dirname'
27
+ end
28
+
29
+ it 'instantiate file from cotta' do
30
+ file = @cotta.file('one/two/three.txt')
31
+ file.name.should == 'three.txt'
32
+ file.parent.name.should == 'two'
33
+ end
34
+
35
+ it 'entry creates file or directory based on which one exists' do
36
+ @cotta.file('file').save
37
+ @cotta.dir('dir').mkdirs
38
+ @cotta.entry('file').should be_exist
39
+ @cotta.entry('dir').should be_exist
40
+ end
41
+
42
+ it 'nil in, nil out' do
43
+ @cotta.file(nil).should be_nil
44
+ @cotta.dir(nil).should be_nil
45
+ Cotta.file(nil).should be_nil
46
+ Cotta.dir(nil).should be_nil
47
+ end
48
+
49
+ it 'create parent directory directly from __FILE__' do
50
+ actual = Cotta.parent_dir(__FILE__)
51
+ actual.name.should == 'cotta'
52
+ end
53
+
54
+ end
@@ -0,0 +1,163 @@
1
+ require File.dirname(__FILE__) + '/../test'
2
+
3
+ module Cotta
4
+ describe 'FileSystemBehaviors', :shared=>true do
5
+ before do
6
+ create_system
7
+ end
8
+
9
+ it 'current directory always exists' do
10
+ @system.dir_exists?(Pathname.new('.')).should == true
11
+ end
12
+
13
+ it 'mkdir should create directory' do
14
+ pathname = Pathname.new('/one')
15
+ @system.dir_exists?(pathname).should == false
16
+ @system.mkdir(pathname)
17
+ @system.dir_exists?(pathname).should == true
18
+ end
19
+
20
+ it 'mkdir raise error if dir already exists' do
21
+ pathname = Pathname.new('/one')
22
+ @system.mkdir(pathname)
23
+ lambda {
24
+ @system.mkdir
25
+ }.should raise_error(StandardError)
26
+ end
27
+
28
+ it 'io returns IO handle' do
29
+ pathname = Pathname.new('file.txt')
30
+ @system.file_exists?(pathname).should == false
31
+ write_io = load_io(pathname, 'w')
32
+ write_io.puts 'content'
33
+ write_io.close
34
+ @system.file_exists?(pathname).should == true
35
+ read_io = load_io(pathname, 'r')
36
+ read_io.gets.should == "content\n"
37
+ read_io.close
38
+ @system.delete_file(pathname)
39
+ end
40
+
41
+ it 'file creation should leave file system consistent' do
42
+ pathname = Pathname.new('dir/sub/file.txt')
43
+ @system.mkdir(pathname.parent.parent)
44
+ @system.mkdir(pathname.parent)
45
+ @system.file_exists?(pathname).should == false
46
+ @system.dir_exists?(pathname.parent).should == true
47
+ load_io(pathname, 'w').close
48
+ @system.file_exists?(pathname).should == true
49
+ @system.dir_exists?(pathname).should == false
50
+ @system.dir_exists?(pathname.parent).should == true
51
+ children = @system.list(pathname.parent)
52
+ children.size.should == 1
53
+ children[0].should == pathname.basename.to_s
54
+ end
55
+
56
+ it 'directory creation should leave file system consistent' do
57
+ pathname = Pathname.new('root/dir/sub')
58
+ @system.dir_exists?(pathname).should == false
59
+ @system.file_exists?(pathname).should == false
60
+ @system.dir_exists?(pathname.parent).should == false
61
+ @system.mkdir(pathname.parent.parent)
62
+ @system.mkdir(pathname.parent)
63
+ @system.mkdir(pathname)
64
+ @system.dir_exists?(pathname).should == true
65
+ @system.file_exists?(pathname).should == false
66
+ @system.dir_exists?(pathname.parent).should == true
67
+ list = @system.list(pathname.parent)
68
+ list.size.should == 1
69
+ list[0].should ==(pathname.basename.to_s)
70
+ end
71
+
72
+ it 'read io should raise error if file does not exists' do
73
+ pathname = Pathname.new('dir/file.txt')
74
+ Proc.new {
75
+ @system.io(pathname, 'r')
76
+ }.should raise_error(Errno::ENOENT)
77
+ end
78
+
79
+ it 'delete dir' do
80
+ pathname = Pathname.new('dir')
81
+ @system.mkdir(pathname)
82
+ @system.delete_dir(pathname)
83
+ @system.dir_exists?(pathname).should == false
84
+ end
85
+
86
+ it 'deleting dir that does not exist should raise error' do
87
+ pathname = Pathname.new('dir/dir2')
88
+ Proc.new {
89
+ @system.delete_dir(pathname)
90
+ }.should raise_error(Errno::ENOENT)
91
+ end
92
+
93
+ it 'copy file' do
94
+ pathname = Pathname.new('file1')
95
+ write_io = load_io(pathname, 'w')
96
+ write_io.puts 'line'
97
+ write_io.close
98
+ target = Pathname.new('target')
99
+ @system.copy_file(pathname, target)
100
+ @system.file_exists?(target).should == true
101
+ read_io = load_io(target, 'r')
102
+ read_io.gets.should == "line\n"
103
+ read_io.close
104
+ end
105
+
106
+ it 'move file' do
107
+ pathname = Pathname.new('file1')
108
+ write_content(pathname, 'line')
109
+ target = Pathname.new('target')
110
+ @system.move_file(pathname, target)
111
+ @system.file_exists?(target).should == true
112
+ read_io = load_io(target, 'r')
113
+ read_io.gets.should == "line\n"
114
+ read_io.close
115
+ @system.file_exists?(pathname).should == false
116
+ end
117
+
118
+ it 'move dir' do
119
+ source = Pathname.new('source')
120
+ @system.mkdir source
121
+ source_file = Pathname.new('source/file.txt')
122
+ write_content(source_file, 'file.txt')
123
+ @system.mkdir source.join('subdir')
124
+ target = Pathname.new('target')
125
+ @system.move_dir(source, target)
126
+ @system.list(target).size.should == 2
127
+ @system.dir_exists?(source).should == false
128
+ @system.dir_exists?(target).should == true
129
+ @system.file_exists?(Pathname.new('target/file.txt')).should == true
130
+ @system.dir_exists?(Pathname.new('target/subdir')).should == true
131
+ end
132
+
133
+ it 'copy dir' do
134
+ source = Pathname.new('source')
135
+ @system.mkdir source
136
+ source_file = Pathname.new('source/file.txt')
137
+ write_content(source_file, 'file.txt')
138
+ @system.mkdir source.join('subdir')
139
+ target = Pathname.new('target')
140
+ @system.copy_dir(source, target)
141
+ @system.list(target).size.should == 2
142
+ @system.dir_exists?(source).should == true
143
+ @system.dir_exists?(target).should == true
144
+ @system.file_exists?(Pathname.new('target/file.txt')).should == true
145
+ end
146
+
147
+ def write_content(pathname, content)
148
+ write_io = load_io(pathname, 'w')
149
+ write_io.puts content
150
+ write_io.close
151
+ end
152
+
153
+ def load_io(*args)
154
+ io = @system.io(*args)
155
+ if (io)
156
+ @ios.push(io)
157
+ else
158
+ raise "IO is null for: #{args}"
159
+ end
160
+ return io
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/../test'
2
+ require File.dirname(__FILE__) + '/file_system_behaviors'
3
+
4
+ module Cotta
5
+
6
+ describe InMemorySystem do
7
+ it_should_behave_like 'FileSystemBehaviors'
8
+
9
+ def create_system
10
+ @system = InMemorySystem.new
11
+ @ios = Array.new
12
+ end
13
+
14
+ after do
15
+ @ios.each {|io| io.close unless io.closed?}
16
+ end
17
+
18
+ it 'root directory always exists' do
19
+ @system.dir_exists?(Pathname.new('/')).should == true
20
+ @system.dir_exists?(Pathname.new('D:/')).should == true
21
+ end
22
+
23
+ end
24
+ end
Binary file
@@ -0,0 +1,20 @@
1
+ require 'spec'
2
+ require File.dirname(__FILE__) + '/../test'
3
+
4
+ describe Pathname do
5
+ it 'parent check for unix path' do
6
+ pathname = Pathname.new('/')
7
+ pathname.cotta_parent.should be_nil
8
+ end
9
+
10
+ it 'parent check for windows path' do
11
+ pathname = Pathname.new('D:/')
12
+ pathname.cotta_parent.should be_nil
13
+ end
14
+
15
+ it 'parent check for normal ppl' do
16
+ pathname = Pathname.new('/test')
17
+ pathname.cotta_parent.should == Pathname.new('/')
18
+ end
19
+
20
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec'
2
+ dir = File.dirname(__FILE__)
3
+ require dir + '/../test'
4
+ require dir + '/file_system_behaviors'
5
+
6
+ module Cotta
7
+
8
+ describe PhysicalSystem do
9
+ it_should_behave_like "FileSystemBehaviors"
10
+ def create_system
11
+ @system = PhysicalSystemStub.new
12
+ @ios = Array.new
13
+ end
14
+
15
+ after do
16
+ @ios.each {|io| io.close unless io.closed?}
17
+ end
18
+
19
+ it 'root directory always exists' do
20
+ @system = PhysicalSystem.new
21
+ @system.dir_exists?(Pathname.new('/')).should == true
22
+ @system.dir_exists?(Pathname.new('D:/')).should == true
23
+ end
24
+
25
+ it 'shell command should return output' do
26
+ @system = PhysicalSystem.new
27
+ @system.shell('ruby --version')[0..3].should == 'ruby'
28
+ end
29
+
30
+ it 'should equals to any other physical system' do
31
+ PhysicalSystem.new.should == PhysicalSystem.new
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,122 @@
1
+ module Cotta
2
+ class PhysicalSystemStub
3
+ attr_reader :executed_commands
4
+
5
+ def initialize
6
+ @executed_commands = Array.new
7
+ tmp_path = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'tmp'))
8
+ @system = PhysicalSystem.new
9
+ ensure_clean_directory(tmp_path)
10
+ Dir.mkdir(File.join(tmp_path, 'current'))
11
+ Dir.mkdir(File.join(tmp_path, 'root'))
12
+ @tmp_path = Pathname.new(tmp_path)
13
+ end
14
+
15
+ def shell(command, &block)
16
+ @executed_commands.push command
17
+ end
18
+
19
+ def dir_exists?(pathname)
20
+ @system.dir_exists?(relative_from_tmp(pathname))
21
+ end
22
+
23
+ def file_exists?(pathname)
24
+ @system.file_exists?(relative_from_tmp(pathname))
25
+ end
26
+
27
+ def dir_stat(pathname)
28
+ @system.dir_stat(relative_from_tmp(pathname))
29
+ end
30
+
31
+ def file_stat(pathname)
32
+ @system.file_stat(relative_from_tmp(pathname))
33
+ end
34
+
35
+ def list(pathname)
36
+ @system.list(relative_from_tmp(pathname))
37
+ end
38
+
39
+ def mkdir(pathname)
40
+ @system.mkdir(relative_from_tmp(pathname))
41
+ end
42
+
43
+ def delete_file(pathname)
44
+ @system.delete_file(relative_from_tmp(pathname))
45
+ end
46
+
47
+ def delete_dir(pathname)
48
+ @system.delete_dir(relative_from_tmp(pathname))
49
+ end
50
+
51
+ def io(pathname, arguments)
52
+ @system.io(relative_from_tmp(pathname), arguments)
53
+ end
54
+
55
+ def copy_file(source, target)
56
+ @system.copy_file(relative_from_tmp(source), relative_from_tmp(target))
57
+ end
58
+
59
+ def move_file(source, target)
60
+ @system.move_file(relative_from_tmp(source), relative_from_tmp(target))
61
+ end
62
+
63
+ def copy_dir(source, target)
64
+ @system.copy_dir(relative_from_tmp(source), relative_from_tmp(target))
65
+ end
66
+
67
+ def move_dir(source, target)
68
+ @system.move_dir(relative_from_tmp(source), relative_from_tmp(target))
69
+ end
70
+
71
+ def copy_dir(source, target)
72
+ @system.copy_dir(relative_from_tmp(source), relative_from_tmp(target))
73
+ end
74
+
75
+ def chdir(path, &block)
76
+ @system.chdir(relative_from_tmp(path), &block)
77
+ end
78
+
79
+ def pwd
80
+ path = @system.pwd
81
+ candidate = relative_from_tmp(Pathname.new('/')).expand_path.to_s
82
+ if (path.index(candidate) == 0)
83
+ result = path[candidate.length, path.length - candidate.length]
84
+ else
85
+ candidate = relative_from_tmp(Pathname.new('.')).expand_path.to_s
86
+ if candidate == path
87
+ result = '.'
88
+ else
89
+ result = path[candidate.length + 1, path.length - candidate.length - 1]
90
+ end
91
+ end
92
+ result
93
+ end
94
+
95
+ private
96
+ def relative_from_tmp(pathname)
97
+ tmp_pathname = nil
98
+ if (pathname.absolute?)
99
+ tmp_pathname = Pathname.new("root#{pathname}")
100
+ else
101
+ tmp_pathname = Pathname.new("current").join(pathname)
102
+ end
103
+ return @tmp_path.join(tmp_pathname)
104
+ end
105
+
106
+ def ensure_clean_directory(path)
107
+ Dir.mkdir path unless File.directory? path
108
+ Dir.foreach(path) do |name|
109
+ if (name != '.' && name != '..')
110
+ child_path = File.join(path, name)
111
+ if (File.directory? child_path)
112
+ ensure_clean_directory(child_path)
113
+ Dir.rmdir(child_path)
114
+ else
115
+ File.delete(child_path)
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ end
122
+ end