@@ -31,3 +31,79 @@ def test_clean_command_works_with_nested_hashes_and_without_any_compiled_files
31
31
end
32
32
end
33
33
end
34
+
35
+ class ClearCommandVersioningTest < Minitest ::Test
36
+ def setup
37
+ @now = Time . parse ( "2021-01-01 12:34:56 UTC" )
38
+ # Test assets to be kept and deleted, path and mtime
39
+ @prev_files = {
40
+ # recent versions to be kept with Webpacker.commands.clean(count = 2)
41
+ "js/application-deadbeef.js" => @now - 4000 ,
42
+ "js/common-deadbeee.js" => @now - 4002 ,
43
+ "css/common-deadbeed.css" => @now - 4004 ,
44
+ "media/images/logo-deadbeeb.css" => @now - 4006 ,
45
+ "js/application-1eadbeef.js" => @now - 8000 ,
46
+ "js/common-1eadbeee.js" => @now - 8002 ,
47
+ "css/common-1eadbeed.css" => @now - 8004 ,
48
+ "media/images/logo-1eadbeeb.css" => @now - 8006 ,
49
+ # new files to be kept with Webpacker.commands.clean(age = 3600)
50
+ "js/brandnew-0001.js" => @now ,
51
+ "js/brandnew-0002.js" => @now - 10 ,
52
+ "js/brandnew-0003.js" => @now - 20 ,
53
+ "js/brandnew-0004.js" => @now - 40 ,
54
+ } . transform_keys { |path | "#{ Webpacker . config . public_output_path } /#{ path } " }
55
+ @expired_files = {
56
+ # old files that are outside count = 2 or age = 3600 and to be deleted
57
+ "js/application-0eadbeef.js" => @now - 9000 ,
58
+ "js/common-0eadbeee.js" => @now - 9002 ,
59
+ "css/common-0eadbeed.css" => @now - 9004 ,
60
+ "js/brandnew-0005.js" => @now - 3640 ,
61
+ } . transform_keys { |path | "#{ Webpacker . config . public_output_path } /#{ path } " }
62
+ @all_files = @prev_files . merge ( @expired_files )
63
+ @dir_glob_stub = Proc . new { |arg |
64
+ case arg
65
+ when "#{ Webpacker . config . public_output_path } /**/*"
66
+ @all_files . keys
67
+ else
68
+ [ ]
69
+ end
70
+ }
71
+ @file_mtime_stub = Proc . new { |longpath |
72
+ @all_files [ longpath ]
73
+ }
74
+ @file_delete_mock = Minitest ::Mock . new
75
+ @expired_files . keys . each do |longpath |
76
+ @file_delete_mock . expect ( :delete , 1 , [ longpath ] )
77
+ end
78
+ @file_delete_stub = Proc . new { |longpath |
79
+ if @prev_files . has_key? ( longpath )
80
+ flunk "#{ longpath } should not be deleted"
81
+ else
82
+ @file_delete_mock . delete ( longpath )
83
+ end
84
+ }
85
+ end
86
+
87
+ def time_and_files_stub ( &proc )
88
+ Time . stub :now , @now do
89
+ Dir . stub :glob , @dir_glob_stub do
90
+ File . stub :directory? , false do
91
+ File . stub :file? , true do
92
+ File . stub :mtime , @file_mtime_stub do
93
+ File . stub :delete , @file_delete_stub do
94
+ yield proc
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ @file_delete_mock . verify
102
+ end
103
+
104
+ def test_clean_command_with_versioned_files
105
+ time_and_files_stub do
106
+ assert Webpacker . commands . clean
107
+ end
108
+ end
109
+ end
0 commit comments