clutil 2010.127.0

Sign up to get free protection for your applications and to get access to all the features.
data/cl/util/test.rb ADDED
@@ -0,0 +1,92 @@
1
+ # $Id: test.rb,v 1.5 2005/04/22 03:59:43 chrismo Exp $
2
+ =begin
3
+ --------------------------------------------------------------------------
4
+ Copyright (c) 2001, Chris Morris
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without modification,
8
+ are permitted provided that the following conditions are met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright notice, this
11
+ list of conditions and the following disclaimer.
12
+
13
+ 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ this list of conditions and the following disclaimer in the documentation and/or
15
+ other materials provided with the distribution.
16
+
17
+ 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
18
+ software may be used to endorse or promote products derived from this software
19
+ without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ --------------------------------------------------------------------------------
32
+ (based on BSD Open Source License)
33
+ =end
34
+
35
+ require 'cl/util/file'
36
+ require 'ftools'
37
+ require 'test/unit'
38
+
39
+ class TempDirTest < Test::Unit::TestCase
40
+ def setTempDir
41
+ @tempDir = '/tmp/tests'
42
+ end
43
+
44
+ def set_up
45
+ @fileNameInc = 0
46
+ setTempDir
47
+ File.makedirs(@tempDir) if !FileTest.directory?(@tempDir)
48
+ end
49
+
50
+ def tear_down
51
+ ClUtilFile.delTree(@tempDir)
52
+ end
53
+
54
+ alias setup set_up
55
+ alias teardown tear_down
56
+
57
+ # to ward off the new Test::Unit detection of classes with no test
58
+ # methods
59
+ def default_test
60
+ super unless(self.class == TempDirTest)
61
+ end
62
+
63
+ def makeSubDir(dirname)
64
+ newdirname = File.join(@tempDir, dirname)
65
+ File.makedirs(newdirname) if !FileTest.directory?(newdirname)
66
+ newdirname
67
+ end
68
+
69
+ def makeSampleTextFile(dirname='', size=0)
70
+ crlfLength = 2
71
+
72
+ if size == 0
73
+ content = 'this is a sample file'
74
+ else
75
+ content = ''
76
+ (size - crlfLength).times do content << 'x' end
77
+ end
78
+
79
+ if dirname.empty?
80
+ sampleFileDir = @tempDir
81
+ else
82
+ sampleFileDir = File.join(@tempDir, dirname)
83
+ end
84
+
85
+ @fileNameInc += 1
86
+ filename = File.join(sampleFileDir, 'sample' + @fileNameInc.to_s + '.txt')
87
+ File.open(filename, File::CREAT|File::TRUNC|File::RDWR) do |f|
88
+ f.puts content
89
+ end
90
+ filename
91
+ end
92
+ end
@@ -0,0 +1,138 @@
1
+ =begin
2
+ --------------------------------------------------------------------------
3
+ Copyright (c) 2001, Chris Morris
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification,
7
+ are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation and/or
14
+ other materials provided with the distribution.
15
+
16
+ 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
17
+ software may be used to endorse or promote products derived from this software
18
+ without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ --------------------------------------------------------------------------------
31
+ =end
32
+
33
+ $LOAD_PATH << '..'
34
+ require 'test'
35
+ require 'dirsize'
36
+ require 'test/unit'
37
+
38
+ TestData = Struct.new("TestData",
39
+ :clusterSize,
40
+ :expectedFileSize,
41
+ :expectedDiskSpace,
42
+ :expectedFileSizeIncludeSubs,
43
+ :expectedDiskSpaceIncludeSubs,
44
+ :expectedFileCount,
45
+ :expectedAvgFileSize,
46
+ :expectedFileCountIncludeSubs,
47
+ :expectedAvgFileSizeIncludeSubs
48
+ )
49
+
50
+ class TestDirSize < TempDirTest
51
+ def set_up
52
+ super
53
+ @testData = TestData.new
54
+ end
55
+
56
+ def testLargerSingleton
57
+ makeSampleTextFile('', 5000)
58
+ @testData.clusterSize = 4096
59
+ @testData.expectedFileSize = 5000
60
+ @testData.expectedDiskSpace = (4096 * 2)
61
+ @testData.expectedFileSizeIncludeSubs = 5000
62
+ @testData.expectedDiskSpaceIncludeSubs = (4096 * 2)
63
+ @testData.expectedFileCount = 1
64
+ @testData.expectedAvgFileSize = 5000
65
+ @testData.expectedFileCountIncludeSubs = 1
66
+ @testData.expectedAvgFileSizeIncludeSubs = 5000
67
+ doTestDirSize(@testData)
68
+ end
69
+
70
+ def doTestDirSize(testData)
71
+ dirSize = DirSize.new
72
+ dirSize.directory = @tempDir
73
+ dirSize.clusterSize = testData.clusterSize
74
+ dirSize.getSize
75
+ assert_equal(testData.expectedFileSize, dirSize.fileSize(false))
76
+ assert_equal(testData.expectedDiskSpace, dirSize.diskSpace(false))
77
+ assert_equal(testData.expectedFileSizeIncludeSubs, dirSize.fileSize(true))
78
+ assert_equal(testData.expectedDiskSpaceIncludeSubs,
79
+ dirSize.diskSpace(true))
80
+ assert_equal(testData.expectedDiskSpace - testData.expectedFileSize,
81
+ dirSize.unusedDiskSpace(false))
82
+ assert_equal(
83
+ (testData.expectedDiskSpaceIncludeSubs -
84
+ testData.expectedFileSizeIncludeSubs), dirSize.unusedDiskSpace(true))
85
+ assert_equal(testData.expectedFileCount, dirSize.fileCount(false))
86
+ assert_equal(testData.expectedAvgFileSize, dirSize.avgFileSize(false))
87
+ assert_equal(testData.expectedFileCountIncludeSubs,
88
+ dirSize.fileCount(true))
89
+ assert_equal(testData.expectedAvgFileSizeIncludeSubs,
90
+ dirSize.avgFileSize(true))
91
+ end
92
+
93
+ def testSmallSingleton
94
+ makeSampleTextFile('', 1000)
95
+ @testData.clusterSize = 4096
96
+ @testData.expectedFileSize = 1000
97
+ @testData.expectedDiskSpace = (4096 * 1)
98
+ @testData.expectedFileSizeIncludeSubs = 1000
99
+ @testData.expectedDiskSpaceIncludeSubs = (4096 * 1)
100
+ @testData.expectedFileCount = 1
101
+ @testData.expectedAvgFileSize = 1000
102
+ @testData.expectedFileCountIncludeSubs = 1
103
+ @testData.expectedAvgFileSizeIncludeSubs = 1000
104
+ doTestDirSize(@testData)
105
+ end
106
+
107
+ def testSubDir
108
+ makeSubDir('suba')
109
+ makeSubDir("suba\\suba1")
110
+ makeSampleTextFile('', 1000)
111
+ makeSampleTextFile('suba', 1000)
112
+ makeSampleTextFile("suba\\suba1", 1000)
113
+ makeSampleTextFile("suba\\suba1", 2000)
114
+ @testData.clusterSize = 4096
115
+ @testData.expectedFileSize = 1000
116
+ @testData.expectedDiskSpace = (4096 * 1)
117
+ @testData.expectedFileSizeIncludeSubs = 5000
118
+ @testData.expectedDiskSpaceIncludeSubs = (4096 * 4)
119
+ @testData.expectedFileCount = 1
120
+ @testData.expectedAvgFileSize = 1000
121
+ @testData.expectedFileCountIncludeSubs = 4
122
+ @testData.expectedAvgFileSizeIncludeSubs = 1250
123
+ doTestDirSize(@testData)
124
+ end
125
+
126
+ def testEmptyDir
127
+ @testData.clusterSize = 4096
128
+ @testData.expectedFileSize = 0
129
+ @testData.expectedDiskSpace = 0
130
+ @testData.expectedFileCount = 0
131
+ @testData.expectedAvgFileSize = 0
132
+ @testData.expectedFileCountIncludeSubs = 0
133
+ @testData.expectedAvgFileSizeIncludeSubs = 0
134
+ @testData.expectedFileSizeIncludeSubs = 0
135
+ @testData.expectedDiskSpaceIncludeSubs = 0
136
+ doTestDirSize(@testData)
137
+ end
138
+ end
@@ -0,0 +1,178 @@
1
+ # $Id: filetest.rb,v 1.3 2002/08/28 16:49:33 chrismo Exp $
2
+ =begin
3
+ --------------------------------------------------------------------------
4
+ Copyright (c) 2001, Chris Morris
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without modification,
8
+ are permitted provided that the following conditions are met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright notice, this
11
+ list of conditions and the following disclaimer.
12
+
13
+ 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ this list of conditions and the following disclaimer in the documentation and/or
15
+ other materials provided with the distribution.
16
+
17
+ 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
18
+ software may be used to endorse or promote products derived from this software
19
+ without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ --------------------------------------------------------------------------------
32
+ (based on BSD Open Source License)
33
+ =end
34
+
35
+ $LOAD_PATH << '..'
36
+ require 'file'
37
+ require 'test/unit'
38
+ require 'ftools'
39
+
40
+ # don't inherit from clutiltest.rb.TempDirTest, it uses things that are tested here
41
+ class TestUtilFile < Test::Unit::TestCase
42
+ READ_WRITE = 0644
43
+ READ_ONLY = 0444
44
+
45
+ def createTestFile(fileName, content, mtime=Time.now, attr=READ_WRITE)
46
+ f = File.new(fileName, File::CREAT|File::RDWR|File::TRUNC)
47
+ f.puts(content)
48
+ f.flush
49
+ f.close
50
+ File.utime(mtime, mtime, fileName)
51
+ File.chmod(attr, fileName)
52
+ end
53
+
54
+ def set_up
55
+ @files = []
56
+ end
57
+
58
+ def teardown
59
+ @files.each { | filename | File.delete_all(filename) if File.exists?(filename) }
60
+ @dirs.reverse_each { | dirname | Dir.delete(dirname) if File.exists?(dirname) }
61
+ end
62
+
63
+ def doTestDelTree(attr)
64
+ @dirs = ['/tmp/utilfiletest',
65
+ '/tmp/utilfiletest/subA',
66
+ '/tmp/utilfiletest/subA/subA1',
67
+ '/tmp/utilfiletest/subA/subA2',
68
+ '/tmp/utilfiletest/subB',
69
+ '/tmp/utilfiletest/subB/subB1',
70
+ '/tmp/utilfiletest/subB/subB1/subB1a']
71
+ @dirs.each { | dirName | File.makedirs(dirName) }
72
+ @files = ['/tmp/utilfiletest/subA/blah.txt',
73
+ '/tmp/utilfiletest/subA/subA1/blah.txt',
74
+ '/tmp/utilfiletest/subA/subA2/blah.txt',
75
+ '/tmp/utilfiletest/subB/blah.txt',
76
+ '/tmp/utilfiletest/subB/subB1/blah.txt',
77
+ '/tmp/utilfiletest/subB/subB1/subB1a/blah.txt']
78
+ @files.each { | filename | createTestFile(filename, 'test content', Time.now, attr) }
79
+ ClUtilFile.delTree(@dirs[0])
80
+ @files.each { | fileName | assert(!File.exists?(fileName)) }
81
+ @dirs.each { | dirName | assert(!File.exists?(dirName)) }
82
+ end
83
+
84
+ def testDelTree
85
+ doTestDelTree(READ_WRITE)
86
+ end
87
+
88
+ def testDelTreeROFiles
89
+ doTestDelTree(READ_ONLY)
90
+ end
91
+
92
+ def testDelTreeFileNameMatch
93
+ @dirs = ['/tmp/utilfiletest',
94
+ '/tmp/utilfiletest/subA',
95
+ '/tmp/utilfiletest/subB']
96
+ @dirs.each { | dirName | File.makedirs(dirName) }
97
+ @filesToStay = ['/tmp/utilfiletest/subA/blah.doc',
98
+ '/tmp/utilfiletest/subB/blah.doc']
99
+ @filesToDelete = ['/tmp/utilfiletest/subA/blah.txt',
100
+ '/tmp/utilfiletest/subB/blah.txt']
101
+ @files << @filesToStay
102
+ @files << @filesToDelete
103
+ @files.flatten!
104
+
105
+ @filesToStay.each { | filename | createTestFile(filename, 'test content') }
106
+ @filesToDelete.each { | filename | createTestFile(filename, 'test content') }
107
+ ClUtilFile.delTree(@dirs[0], '*.txt')
108
+ @filesToStay.each { | fileName | assert(File.exists?(fileName)) }
109
+ @filesToDelete.each { | fileName | assert(!File.exists?(fileName)) }
110
+ ClUtilFile.delTree(@dirs[0])
111
+ @filesToStay.each { | fileName | assert(!File.exists?(fileName)) }
112
+ @filesToDelete.each { | fileName | assert(!File.exists?(fileName)) }
113
+ @dirs.each { | dirName | assert(!File.exists?(dirName)) }
114
+ end
115
+
116
+ def testDelTreeAging
117
+ @dirs = ['/tmp/utilfiletest',
118
+ '/tmp/utilfiletest/subA',
119
+ '/tmp/utilfiletest/subB']
120
+ @dirs.each { | dirName | File.makedirs(dirName) }
121
+ @filesToStay = ['/tmp/utilfiletest/subA/blah0.txt',
122
+ '/tmp/utilfiletest/subB/blah1.txt']
123
+ @filesToDelete = ['/tmp/utilfiletest/subA/blah2.txt',
124
+ '/tmp/utilfiletest/subB/blah3.txt']
125
+ @files << @filesToStay
126
+ @files << @filesToDelete
127
+ @files.flatten!
128
+
129
+ @filesToStay.each { | filename | createTestFile(filename, 'test content') }
130
+
131
+ day = 60 * 60 * 24
132
+ eightDays = day * 8
133
+ sevenDays = day * 7
134
+ @filesToDelete.each { | filename | createTestFile(filename, 'test content', Time.now - eightDays) }
135
+
136
+ ClUtilFile.delTree(@dirs[0]) { | fileName |
137
+ (File.mtime(fileName) < (Time.now - sevenDays))
138
+ }
139
+
140
+ @filesToStay.each { | fileName | assert(File.exists?(fileName)) }
141
+ @filesToDelete.each { | fileName | assert(!File.exists?(fileName)) }
142
+ ClUtilFile.delTree(@dirs[0])
143
+ @filesToStay.each { | fileName | assert(!File.exists?(fileName)) }
144
+ @filesToDelete.each { | fileName | assert(!File.exists?(fileName)) }
145
+ @dirs.each { | dirName | assert(!File.exists?(dirName)) }
146
+ end
147
+
148
+ def testDirFiles
149
+ @dirs = ['/tmp/utilfiletest',
150
+ '/tmp/utilfiletest/subA',
151
+ '/tmp/utilfiletest/subA/subA1',
152
+ '/tmp/utilfiletest/subA/subA2',
153
+ '/tmp/utilfiletest/subB',
154
+ '/tmp/utilfiletest/subB/subB1',
155
+ '/tmp/utilfiletest/subB/subB1/subB1a']
156
+ @dirs.each { | dirName | File.makedirs(dirName) }
157
+ @files = ['/tmp/utilfiletest/subA/blah.txt',
158
+ '/tmp/utilfiletest/subA/subA1/blah.txt',
159
+ '/tmp/utilfiletest/subA/subA2/blah.txt',
160
+ '/tmp/utilfiletest/subB/blah.txt',
161
+ '/tmp/utilfiletest/subB/subB1/blah.txt',
162
+ '/tmp/utilfiletest/subB/subB1/subB1a/blah.txt']
163
+ @files.each { | filename | createTestFile(filename, 'test content') }
164
+ #puts 'Dir.entries'
165
+ #puts Dir.entries('/tmp/utilfiletest/subA/subA1/')
166
+ #puts 'Dir.files'
167
+ #puts Dir.files('/tmp/utilfiletest/subA/subA1/')
168
+ assert_equal(["blah.txt"], Dir.files('/tmp/utilfiletest/subA/subA1/'))
169
+ end
170
+
171
+ def testFileExtension
172
+ assert_equal('rb', File.extension('/test/file.rb'))
173
+ assert_equal(nil, File.extension('/test/file'))
174
+ assert_equal('rb', File.extension('/test/file.of.some.such.rb'))
175
+ end
176
+ end
177
+
178
+
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH << '..'
2
+
3
+ require 'install'
4
+ require 'test/unit'
5
+
6
+ class TestInstall < Test::Unit::TestCase
7
+ def test_set_prefix
8
+ conf = get_conf({'prefix' => '/tmp/ruby'})
9
+ assert_equal('/tmp/ruby', conf['prefix'])
10
+ assert_equal('/tmp/ruby/lib/ruby/site_ruby', conf['sitedir'])
11
+ end
12
+ end
@@ -0,0 +1,43 @@
1
+ # $Id: progresstest.rb,v 1.2 2003/09/12 14:53:53 chrismo Exp $
2
+ =begin
3
+ --------------------------------------------------------------------------
4
+ Copyright (c) 2002, Chris Morris
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without modification,
8
+ are permitted provided that the following conditions are met:
9
+
10
+ 1. Redistributions of source code must retain the above copyright notice, this
11
+ list of conditions and the following disclaimer.
12
+
13
+ 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ this list of conditions and the following disclaimer in the documentation and/or
15
+ other materials provided with the distribution.
16
+
17
+ 3. Neither the names Chris Morris, cLabs nor the names of contributors to this
18
+ software may be used to endorse or promote products derived from this software
19
+ without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
22
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
25
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ --------------------------------------------------------------------------------
32
+ =end
33
+
34
+ $LOAD_PATH << '..'
35
+ require 'progress'
36
+
37
+ p = Progress.new(10)
38
+ p.start
39
+ (0..9).each do |x| puts p.progress(true); sleep 0.5 end
40
+
41
+ p = Progress.new(10)
42
+ p.start
43
+ (0..9).each do |x| print p.in_place_pct(true); sleep 0.5 end