fun_with_files 0.0.14 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.markdown +15 -3
  3. data/Gemfile +17 -7
  4. data/{README.rdoc → README.markdown} +12 -11
  5. data/Rakefile +3 -3
  6. data/VERSION +1 -1
  7. data/lib/fun_with/files/bootstrapper.rb +87 -0
  8. data/lib/fun_with/files/core_extensions/file.rb +15 -3
  9. data/lib/fun_with/files/core_extensions/set.rb +12 -0
  10. data/lib/fun_with/files/core_extensions/true_class.rb +11 -0
  11. data/lib/fun_with/files/digest_methods.rb +50 -17
  12. data/lib/fun_with/files/directory_builder.rb +9 -4
  13. data/lib/fun_with/files/downloader.rb +29 -16
  14. data/lib/fun_with/files/errors.rb +9 -1
  15. data/lib/fun_with/files/file_manipulation_methods.rb +25 -15
  16. data/lib/fun_with/files/file_orderer.rb +2 -0
  17. data/lib/fun_with/files/file_path.rb +242 -156
  18. data/lib/fun_with/files/file_path_class_methods.rb +23 -2
  19. data/lib/fun_with/files/file_permission_methods.rb +18 -7
  20. data/lib/fun_with/files/file_requirements.rb +63 -7
  21. data/lib/fun_with/files/requirements/manager.rb +104 -0
  22. data/lib/fun_with/files/root_path.rb +3 -3
  23. data/lib/fun_with/files/stat_methods.rb +33 -0
  24. data/lib/fun_with/files/string_behavior.rb +6 -2
  25. data/lib/fun_with/files/utils/byte_size.rb +143 -0
  26. data/lib/fun_with/files/utils/opts.rb +26 -0
  27. data/lib/fun_with/files/utils/succession.rb +47 -0
  28. data/lib/fun_with/files/utils/timestamp.rb +47 -0
  29. data/lib/fun_with/files/utils/timestamp_format.rb +31 -0
  30. data/lib/fun_with/files/watcher.rb +157 -0
  31. data/lib/fun_with/files/watchers/directory_watcher.rb +67 -0
  32. data/lib/fun_with/files/watchers/file_watcher.rb +45 -0
  33. data/lib/fun_with/files/watchers/missing_watcher.rb +23 -0
  34. data/lib/fun_with/files/watchers/node_watcher.rb +44 -0
  35. data/lib/fun_with/testing/assertions/fun_with_files.rb +91 -0
  36. data/lib/fun_with/testing/test_case_extensions.rb +12 -0
  37. data/lib/fun_with_files.rb +5 -31
  38. data/test/helper.rb +13 -5
  39. data/test/test_core_extensions.rb +6 -0
  40. data/test/test_descent.rb +2 -2
  41. data/test/test_directory_builder.rb +29 -10
  42. data/test/test_extension_methods.rb +62 -0
  43. data/test/test_file_manipulation.rb +4 -4
  44. data/test/test_file_path.rb +83 -56
  45. data/test/test_file_requirements.rb +36 -0
  46. data/test/test_fun_with_files.rb +1 -1
  47. data/test/test_fwf_assertions.rb +62 -0
  48. data/test/test_moving_files.rb +111 -0
  49. data/test/test_permission_methods.rb +22 -0
  50. data/test/test_root_path.rb +9 -0
  51. data/test/test_stat_methods.rb +17 -0
  52. data/test/test_timestamping.rb +74 -0
  53. data/test/test_utils_bytesize.rb +71 -0
  54. data/test/test_utils_succession.rb +30 -0
  55. data/test/test_watchers.rb +196 -0
  56. metadata +59 -13
  57. /data/lib/fun_with/files/core_extensions/{false.rb → false_class.rb} +0 -0
  58. /data/lib/fun_with/files/core_extensions/{nil.rb → nil_class.rb} +0 -0
@@ -13,7 +13,7 @@ class TestFileManipulation < FunWith::Files::TestCase
13
13
 
14
14
  should "copy LICENSE.txt" do
15
15
  copied = @license.cp( "test", "tmp" )
16
- assert_match /LICENSE\.txt/, copied.to_s
16
+ assert_match( /LICENSE\.txt/, copied.to_s )
17
17
  assert_file copied
18
18
  assert_file_has_content copied
19
19
  end
@@ -21,9 +21,9 @@ class TestFileManipulation < FunWith::Files::TestCase
21
21
  should "gsub copy of license.txt" do
22
22
  copied = @license.cp( "test", "tmp" )
23
23
  copied.file_gsub!( /Bryce Anderson/, "Wilford Brimley" )
24
- assert copied.size > 1000
24
+ assert copied.size > 1000, "File is too small"
25
25
 
26
- assert_file_contents copied, /Wilford Brimley/
26
+ assert_file_contents copied, /Wilford Brimley/, "Error 441: Wilford Brimley not found"
27
27
  end
28
28
 
29
29
  should "empty files and directories" do
@@ -120,7 +120,7 @@ class TestFileManipulation < FunWith::Files::TestCase
120
120
 
121
121
  file_def = file_abc.cp( dir_def )
122
122
 
123
- assert_match /abc\.txt/, file_def.path
123
+ assert_match( /abc\.txt/, file_def.path )
124
124
  assert_file_contents file_def, /rent/
125
125
 
126
126
 
@@ -10,17 +10,17 @@ class TestFilePath < FunWith::Files::TestCase
10
10
  end
11
11
 
12
12
  should "have location class methods available" do
13
- assert_respond_to( FunWith::Files::FilePath, :home )
14
- assert_respond_to( FunWith::Files::FilePath, :config_dir )
15
- assert_respond_to( FunWith::Files::FilePath, :root )
16
- assert_respond_to( FunWith::Files::FilePath, :data_dir )
13
+ assert_respond_to FunWith::Files::FilePath, :home
14
+ assert_respond_to FunWith::Files::FilePath, :config_dir
15
+ assert_respond_to FunWith::Files::FilePath, :root
16
+ assert_respond_to FunWith::Files::FilePath, :data_dir
17
17
  end
18
18
 
19
19
 
20
20
  should "join smoothly" do
21
21
  bin_dir = "/bin".fwf_filepath
22
- assert_equal( "/bin/bash", bin_dir.join("bash").to_s )
23
- assert_equal( "/bin/bash", bin_dir.join("bash".fwf_filepath).to_s )
22
+ assert_equal "/bin/bash", bin_dir.join("bash").to_s
23
+ assert_equal "/bin/bash", bin_dir.join("bash".fwf_filepath).to_s
24
24
  end
25
25
 
26
26
  should "go up/down when asked" do
@@ -32,7 +32,7 @@ class TestFilePath < FunWith::Files::TestCase
32
32
  assert_equal root, f1.up.up.up.up.up.up.up
33
33
 
34
34
  #invoking up didn't change original
35
- assert_match /ask_for_floyd/, f1.to_s
35
+ assert_match( /ask_for_floyd/, f1.to_s )
36
36
 
37
37
  assert_equal f1, f2.down( "monkeylips" ).down( "ask_for_floyd" )
38
38
  assert_equal f1, f2.down( "monkeylips", "ask_for_floyd" )
@@ -99,17 +99,20 @@ class TestFilePath < FunWith::Files::TestCase
99
99
  should "sequence files nicely" do
100
100
  seqfile = @tmp_dir.join("sequence.txt")
101
101
 
102
+ files = [seqfile]
103
+
102
104
  10.times do |i|
103
105
  seqfile.write( i.to_s )
104
106
  seqfile = seqfile.succ
107
+ files << seqfile
105
108
  end
106
109
 
107
- assert_file @tmp_dir.join("sequence.txt")
108
- assert_file @tmp_dir.join("sequence.000000.txt")
109
- assert_file @tmp_dir.join("sequence.000008.txt")
110
+ assert_file files[0]
111
+ assert_file files[1]
112
+ assert_file files[9]
110
113
 
111
- assert_file_contents @tmp_dir.join("sequence.txt"), "0"
112
- assert_file_contents @tmp_dir.join("sequence.000008.txt"), "9"
114
+ assert_file_contents files[0], "0"
115
+ assert_file_contents files[9], "9"
113
116
  end
114
117
 
115
118
  should "sequence files with custom stamp length" do
@@ -128,35 +131,7 @@ class TestFilePath < FunWith::Files::TestCase
128
131
  assert_file_contents @tmp_dir.join("sequence.0008.txt"), "9"
129
132
  end
130
133
 
131
- should "sequence files with datestamps" do
132
- seqfile = @tmp_dir.join("sequence.txt")
133
-
134
- 10.times do |i|
135
- seqfile.write( i.to_s )
136
- seqfile = seqfile.succ( timestamp: true )
137
- sleep(0.002)
138
- end
139
-
140
- files = seqfile.succession( timestamp: true )
141
- assert files.length == 10
142
-
143
- files.each_with_index do |file, i|
144
- assert_file file
145
- assert_file_contents file, i.to_s
146
- end
147
-
148
- file_name_strings = files.map(&:to_s)
149
- assert_equal file_name_strings[1..-1], file_name_strings[1..-1].sort
150
- end
151
134
 
152
- should "timestamp files using the timestamp() method" do
153
- timestampable_file = @tmp_dir.join( "timestamped.dat" )
154
- timestamped_file1 = timestampable_file.timestamp
155
- timestamped_file2 = timestampable_file.timestamp("%Y")
156
-
157
- assert timestamped_file1 =~ /timestamped.\d{17}.dat$/
158
- assert timestamped_file2 =~ /timestamped.\d{4}.dat$/
159
- end
160
135
  end
161
136
 
162
137
 
@@ -216,34 +191,86 @@ class TestFilePath < FunWith::Files::TestCase
216
191
  end
217
192
  end
218
193
 
219
- context "test ext()" do
194
+ context "testing extension methods" do
220
195
  setup do
221
- @path = "hello.txt".fwf_filepath
196
+ @hello_path = "hello.txt".fwf_filepath
197
+ @dot_path0 = ".config".fwf_filepath
198
+ @dot_path1 = "~/.ssh".fwf_filepath
222
199
  end
223
200
 
224
- should "not change path when sent nil as an argument" do
225
- assert_equal @path.path, @path.ext( nil ).path
226
- end
201
+ context "test ext()" do
227
202
 
228
- should "append when given symbol" do
229
- assert_equal @path.path + ".tgz", @path.ext( :tgz ).path
230
- end
203
+ should "not change path when sent nil as an argument" do
204
+ assert_equal @hello_path.path, @hello_path.ext( nil ).path
205
+ end
206
+
207
+ should "append when given symbol" do
208
+ assert_equal @hello_path.path + ".tgz", @hello_path.ext( :tgz ).path
209
+ end
231
210
 
232
- should "append when given string" do
233
- assert_equal @path.path + ".tgz", @path.ext( 'tgz' ).path
211
+ should "append when given string" do
212
+ assert_equal @hello_path.path + ".tgz", @hello_path.ext( 'tgz' ).path
213
+ end
214
+
215
+ should "append correctly when given leading ." do
216
+ assert_equal @hello_path.path + ".tgz", @hello_path.ext( '.tgz' ).path
217
+ end
218
+
219
+ should "append multiple extensions as separate args" do
220
+ assert_equal @hello_path.path + ".backup.tar.gz", @hello_path.ext( :backup, "tar", nil, ".gz" ).path
221
+ end
222
+
223
+ should "append multiple extensions as a single string" do
224
+ assert_equal @hello_path.path + ".backup.tar.gz", @hello_path.ext( ".backup.tar.gz" ).path
225
+ end
234
226
  end
235
227
 
236
- should "append correctly when given leading ." do
237
- assert_equal @path.path + ".tgz", @path.ext( '.tgz' ).path
228
+ context "test without_ext()" do
229
+ setup do
230
+
231
+ end
232
+
233
+ should "pop the extension (normal)" do
234
+ #debugger
235
+ assert_equal "hello", @hello_path.without_ext.to_s
236
+ assert_equal "hello", @hello_path.without_ext("txt").path
237
+ assert_equal "hello", @hello_path.without_ext(".txt").path
238
+ assert_equal "hello.txt", @hello_path.without_ext(".html").path
239
+ assert_equal "hello.txt", @hello_path.without_ext("html").path
240
+
241
+ assert_equal "hello", @hello_path.without_ext(:txt).path
242
+ end
243
+
244
+ should "not affect dot files" do
245
+ assert_equal ".config", @dot_path0.without_ext.path
246
+ assert_equal ".config", @dot_path0.without_ext("config").path
247
+ assert_equal ".config", @dot_path0.without_ext(".config").path
248
+ assert_equal ".config", @dot_path0.without_ext(:config).path
249
+ end
238
250
  end
239
251
 
240
- should "append multiple extensions as separate args" do
241
- assert_equal @path.path + ".backup.tar.gz", @path.ext( :backup, "tar", nil, ".gz" ).path
252
+ # Why does it return a filepath for the dirname piece, strings for the other two pieces?
253
+ context "test dirname_and_basename_and_ext" do
254
+ end
255
+ end
256
+
257
+ context "test join()" do
258
+ setup do
259
+ @path = "/".fwf_filepath
242
260
  end
243
261
 
244
- should "append multiple extensions as a single string" do
245
- assert_equal @path.path + ".backup.tar.gz", @path.ext( ".backup.tar.gz" ).path
262
+ should "accept all manner of arguments" do
263
+ expected = "/bin/0/file.rb".fwf_filepath
264
+ result = @path.join( :bin, 0, "file.rb" )
265
+
266
+ assert_equal expected, result
267
+
268
+ result = @path / :bin / 0 / "file.rb"
269
+ assert_equal expected, result
246
270
  end
271
+ end
272
+
273
+ context "testing block form" do
247
274
 
248
275
  end
249
276
  end
@@ -0,0 +1,36 @@
1
+ require 'helper'
2
+
3
+ class TestFileRequirements < FunWith::Files::TestCase
4
+ context "testing FileRequirements methods" do
5
+ setup do
6
+ @tmp_dir = FunWith::Files.root("test", "tmp")
7
+ @file = @tmp_dir.join("file.txt").touch
8
+ @dir = @tmp_dir.join("dir").touch_dir
9
+ end
10
+
11
+ teardown do
12
+ empty_temp_directory
13
+ end
14
+
15
+ context "needs_to_exist()" do
16
+ should "raise an error when a file doesn't exist" do
17
+ assert_raises Errno::ENOENT do
18
+ @tmp_dir.join("missing_file.txt").needs_to_exist
19
+ end
20
+ end
21
+
22
+ should "raise an error when a file isn't empty" do
23
+ assert_raises Errors::FileNotEmpty do
24
+ @file.append("Zorpy was here")
25
+ @file.needs_to_be_empty
26
+ end
27
+ end
28
+
29
+ should "raise an error when a file oughta be a directory" do
30
+ assert_raises Errors::NotADirectory do
31
+ @file.must_be_directory
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -26,7 +26,7 @@ class TestFunWithFiles < FunWith::Files::TestCase
26
26
  assert_respond_to( FunWith::Files, :root )
27
27
  assert_respond_to( FunWith::Files, :version )
28
28
 
29
- assert_equal "0.0.13", FunWith::Files.version # Gotta change with every point release. Ick.
29
+ assert_equal "0.0.17", FunWith::Files.version # Gotta change with every point release. Ick.
30
30
  end
31
31
  end
32
32
  end
@@ -0,0 +1,62 @@
1
+ module FunWith
2
+ module Testing
3
+ class TestFwfAssertions < FunWith::Testing::AssertionsTestCase
4
+ context "testing assertions" do
5
+ setup do
6
+ extended_test_case # sets @case, which is used to access to assertions
7
+ @case_class.install_fun_with_files_assertions
8
+ end
9
+
10
+ context "testing :assert_fwf_filepath()" do
11
+ should "pass all tests" do
12
+ testing_method :assert_fwf_filepath do
13
+ nope __FILE__
14
+ yep __FILE__.fwf_filepath
15
+
16
+ nope nil
17
+ nope :five
18
+ nope 5
19
+ nope [5]
20
+ nope "five"
21
+ end
22
+ end
23
+ end
24
+
25
+ context "testing :assert_file()" do
26
+ should "pass all tests" do
27
+ testing_method :assert_file do
28
+ yep __FILE__.fwf_filepath
29
+
30
+ nope __FILE__
31
+ nope nil
32
+ nope :five
33
+ nope 5
34
+ nope [5]
35
+ nope "five"
36
+ end
37
+ end
38
+ end
39
+
40
+ context "testing :assert_directory()" do
41
+ should "pass all tests" do
42
+ testing_method :assert_directory do
43
+ nope __FILE__
44
+ nope __FILE__.fwf_filepath
45
+
46
+ yep __FILE__.fwf_filepath.dirname
47
+ yep __FILE__.fwf_filepath.up
48
+ yep FunWith::Files.root
49
+
50
+ nope nil
51
+ nope :five
52
+ nope 5
53
+ nope [5]
54
+ nope "five"
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+
@@ -0,0 +1,111 @@
1
+ require 'helper'
2
+
3
+ class TestMovingFiles < FunWith::Files::TestCase
4
+ context "inside a tmpdir" do
5
+ setup do
6
+ @src_dir = FilePath.tmpdir
7
+ @dst_dir = FilePath.tmpdir
8
+
9
+ assert_directory @src_dir
10
+ assert_directory @dst_dir
11
+
12
+ assert_empty_directory @src_dir
13
+ assert_empty_directory @dst_dir
14
+ end
15
+
16
+ teardown do
17
+ @src_dir.rm
18
+ @dst_dir.rm
19
+ assert_not_directory @src_dir
20
+ assert_not_directory @dst_dir
21
+ end
22
+
23
+ context "with a source file" do
24
+ setup do
25
+ @src_file = @src_dir / "file.txt"
26
+ @src_file.write( "Hello world" )
27
+
28
+ assert_file_not_empty( @src_file )
29
+ end
30
+
31
+
32
+ should "successfully move a file into a directory" do
33
+ dest = @dst_dir / "file.txt"
34
+
35
+ assert_no_file dest
36
+
37
+ @src_file.move @dst_dir
38
+
39
+ assert_file dest
40
+ end
41
+
42
+
43
+
44
+ # Seems dangerous to not have a concrete idea of what should happen when a move
45
+ # remove / create request takes place. Ideas:
46
+ # be able to mark a destination as a directory, so that it knows the file move
47
+ # is saying to
48
+ #
49
+ # a directory should be created
50
+ # a directory must exist for the move to occur
51
+ # nothing exists at the destination, so the file is given the name of <thing_what_didnt_exist>
52
+ #
53
+ should "fail to move a file to a non-existent directory" do
54
+ flunk "this actually moves the file (the file getting the name of the 'missing' directory, and I'm not sure that's wrong)"
55
+ not_a_dir = @dst_dir / "humblebrag"
56
+
57
+ assert_raises( Errno::ENOENT ) do
58
+ @src_file.move( not_a_dir )
59
+ end
60
+ end
61
+
62
+ should "fail to move a file owing to lack of privileges" do
63
+ write_protected_dir = @dst_dir / "write_protected_dir"
64
+ write_protected_dir.touch_dir
65
+
66
+ temporarily_write_protect( write_protected_dir ) do
67
+ assert_raises( Errno::EACCES ) do
68
+ @src_file.move( write_protected_dir )
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+
75
+
76
+ should "fail to move a non-existent file" do
77
+ f = @src_dir.join( "file.txt" )
78
+
79
+ assert_no_file( f )
80
+
81
+ assert_raises( Errno::ENOENT ) do
82
+ f.move( @dst_dir )
83
+ end
84
+ end
85
+ end
86
+
87
+ #
88
+ # should "successfully move a directory" do
89
+ # flunk "write test"
90
+ # end
91
+ #
92
+ # should "fail to move a non-existent directory" do
93
+ # flunk "write test"
94
+ # end
95
+ #
96
+ # should "fail to move a directory to a non-existent directory" do
97
+ # flunk "write test"
98
+ # end
99
+ #
100
+ # should "fail to move a directory owing to lack of privileges" do
101
+ #
102
+ # flunk "write test"
103
+ # end
104
+ # end
105
+
106
+ def temporarily_write_protect( f, &block )
107
+ f.chmod( "a-w" )
108
+ yield
109
+ f.chmod( "a+w" )
110
+ end
111
+ end
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ class TestPermissionMethods < FunWith::Files::TestCase
4
+ context "checking for availability of permission methods" do
5
+ setup do
6
+ @filepath = FunWith::Files::FilePath.new("/")
7
+ end
8
+
9
+ should "have permission methods" do
10
+ assert_respond_to @filepath, :readable?
11
+ assert_respond_to @filepath, :writable?
12
+ assert_respond_to @filepath, :executable?
13
+ assert_respond_to @filepath, :chown
14
+ assert_respond_to @filepath, :chmod
15
+ assert_respond_to @filepath, :owner
16
+ end
17
+
18
+ should "have a root owner" do
19
+ assert_equal "root", @filepath.owner
20
+ end
21
+ end
22
+ end
@@ -13,6 +13,15 @@ class TestRootPath < FunWith::Files::TestCase
13
13
  rootify_and_test( obj, path )
14
14
  end
15
15
 
16
+ context "FunWith::Files.root" do
17
+ should "be a directory" do
18
+ assert_directory( FunWith::Files.root )
19
+ assert_empty_directory( FunWith::Files.root( :test, :tmp ) )
20
+ assert_empty_directory( FunWith::Files.root / :test / :tmp )
21
+ assert_empty_directory( FunWith::Files.root / "test" / "tmp" )
22
+ end
23
+ end
24
+
16
25
  def rootify_and_test( obj, path )
17
26
  RootPath.rootify( obj, path )
18
27
  assert obj.respond_to?(:root)
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ class TestStatMethods < FunWith::Files::TestCase
4
+
5
+ context "checking for availability of stat-enabling methods" do
6
+ setup do
7
+ @filepath = FunWith::Files::FilePath.new("/")
8
+ end
9
+
10
+ should "have stat methods" do
11
+ assert_respond_to @filepath, :stat
12
+ assert_respond_to @filepath, :inode
13
+ assert_respond_to @filepath, :birthtime # this (and many others) come from Pathname
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,74 @@
1
+ require 'helper'
2
+
3
+ class TestTimestamping < FunWith::Files::TestCase
4
+ context "testing timestamping" do
5
+ setup do
6
+ @tmp_dir = FunWith::Files.root( 'test', 'tmp' )
7
+ @logfile = @tmp_dir / "apache.log"
8
+ @stamp_time = Time.new( 2000, 10, 13, 23, 59, 59 )
9
+ end
10
+
11
+ teardown do
12
+ `rm -rf #{@tmp_dir.join('*')}`
13
+ end
14
+
15
+ should "sequence files with datestamps" do
16
+ dates = %w(2012-11-30 1900-12-01 2727-06-14)
17
+
18
+ for date in dates
19
+ d = Date.new( * date.split("-").map(&:to_i) )
20
+
21
+ f = @logfile.timestamp( format: :ymd, time: d )
22
+ f.write( date )
23
+
24
+ fy = @logfile.timestamp( format: :y, time: d )
25
+ fy.write date[0..3]
26
+ end
27
+
28
+ for str in dates + %w(2012 1900 2727)
29
+ file = @tmp_dir / "apache.#{str}.log"
30
+ assert_file file
31
+ assert_file_contents file, str
32
+ end
33
+ end
34
+
35
+ should "timestamp files using the timestamp() method" do
36
+ timestampable_file = @tmp_dir / "timestamped.dat"
37
+
38
+ timestamped_file1 = timestampable_file.timestamp
39
+ timestamped_file2 = timestampable_file.timestamp( format: :y )
40
+
41
+ assert timestamped_file1 =~ /timestamped.\d{17}.dat$/
42
+ assert timestamped_file2 =~ /timestamped.\d{4}.dat$/
43
+ end
44
+
45
+ should "raise an error when invalid format requested" do
46
+ f = @tmp_dir / "apache.log"
47
+
48
+ stamped = f.timestamp
49
+
50
+ assert_raises Errors::TimestampFormatUnrecognized do
51
+ f.timestamp( format: :zztop )
52
+ end
53
+
54
+ # Symbols only!
55
+ assert_raises Errors::TimestampFormatUnrecognized do
56
+ f.timestamp( format: "ymd" )
57
+ end
58
+ end
59
+
60
+ should "update the timestamp of a file that already has one" do
61
+ f = "apache.19931020235959000.tgz".fwf_filepath
62
+ timestamped_file = f.timestamp( time: @stamp_time )
63
+
64
+ assert_equal "apache.20001013235959000.tgz", timestamped_file.path
65
+ end
66
+
67
+ should "be able to give the timestamp method a custom format" do
68
+ fmt = Utils::TimestampFormat.new.recognizer( /^\d{2}_\d{2}_\d{2}$/ ).strftime( "%m_%d_%y" )
69
+
70
+ timestamped_file = @logfile.timestamp( format: fmt, time: @stamp_time )
71
+ assert_equal "apache.10_13_00.log", timestamped_file.basename.path
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,71 @@
1
+ require 'helper'
2
+
3
+ class TestUtilsByteSize < FunWith::Files::TestCase
4
+ context "testing the thing" do
5
+ setup do
6
+ @bytie = Object.new
7
+ @bytie.extend FunWith::Files::Utils::ByteSize
8
+ end
9
+
10
+ should "respond to :to_bytes" do
11
+ assert_respond_to @bytie, :to_bytes
12
+ end
13
+
14
+ should "accurately convert strings to bytes" do
15
+ assert_bytes 1_000, "1000b"
16
+ assert_bytes 1_000, "1 kb"
17
+ assert_bytes 1_000, "0.001 MB"
18
+ assert_bytes 1_000, "0.000001 GB"
19
+
20
+ assert_bytes 1_234, "1234"
21
+ assert_bytes 1_234, " 1.234 kb "
22
+ assert_bytes 9_001, "9.001k" # it's over 9000!
23
+ end
24
+
25
+ context "converting expressions between units" do
26
+ should "handle simple cases" do
27
+ assert_converts "1KB", "B", "1000B"
28
+ assert_converts "2MB", "KB", "2000KB"
29
+ assert_converts "3GB", "MB", "3000MB"
30
+ assert_converts "4TB", "GB", "4000GB"
31
+ assert_converts "5PB", "TB", "5000TB"
32
+ end
33
+
34
+ should "be case insensitive" do
35
+ assert_converts "1kb", "B", "1000B"
36
+ assert_converts "1000k", "mb", "1mb"
37
+ assert_converts "1000m", "GB", "1GB"
38
+ assert_converts "2000 PB", " EB", "2 EB"
39
+ end
40
+
41
+ should "sometimes put space between number and unit" do
42
+ assert_converts "2000 PB", " EB", "2 EB"
43
+ end
44
+
45
+ should "reflect the unit styling that the caller sends" do
46
+ assert_converts "1kb", "b", "1000b" # uses the unit capitalization that the caller sends
47
+ end
48
+
49
+ should "sometimes use decimal points" do
50
+ assert_converts "900kb", "MB", "0.9MB"
51
+ assert_converts "930kb", "mb", "0.9mb"
52
+
53
+ assert_converts "99500b", "kb", "99.5kb"
54
+ assert_converts "1200MB", "gb", "1.2gb"
55
+ end
56
+
57
+ should "sometimes not use decimal points" do
58
+ assert_converts "100372b", "k", "100k"
59
+ assert_converts "1b", "GB", "0GB"
60
+ end
61
+ end
62
+ end
63
+
64
+ def assert_bytes( n, expr )
65
+ assert_equal n, @bytie.to_bytes( expr ), "to_bytes( #{expr} ) should resolve to #{n}"
66
+ end
67
+
68
+ def assert_converts( old_expr, new_units, new_expr )
69
+ assert_equal new_expr, @bytie.convert( old_expr, new_units )
70
+ end
71
+ end
@@ -0,0 +1,30 @@
1
+ require 'helper'
2
+
3
+ class TestUtilsSuccession < FunWith::Files::TestCase
4
+ USucc = FunWith::Files::Utils::Succession
5
+
6
+ context "testing Succession.get_successor_name()" do
7
+ should "succeed" do
8
+ with_digit_count( 4 ) do
9
+ assert_succession "file.0001.txt", "file.0002.txt"
10
+ assert_succession "file.txt", "file.0000.txt"
11
+ assert_succession "", "0000"
12
+ end
13
+ end
14
+ end
15
+
16
+ def with_digit_count( i, &block )
17
+ @digit_count = i
18
+ yield
19
+ end
20
+
21
+ def assert_succession( input, expected )
22
+ if defined?( @digit_count )
23
+ actual = USucc.get_successor_name( input, @digit_count )
24
+ else
25
+ actual = USucc.get_successor_name( input )
26
+ end
27
+
28
+ assert_equal( expected, actual, "Utils::Succession.get_successor_name() failed:\n\tinput: #{input}(#{input.class})\n\texpected: #{expected}(#{expected.class})\n\tactual: #{actual}(#{actual.class})")
29
+ end
30
+ end