@@ -133,46 +133,7 @@ func TestCheckFilesFilterDateKeywords(t *testing.T) {
133133 snc := StartTestAgent (t , "" )
134134 tmpPath := t .TempDir ()
135135
136- // 1. Prepare files for today vs yesterday
137- now := time .Now ()
138- midnight := time .Date (now .Year (), now .Month (), now .Day (), 0 , 0 , 0 , 0 , now .Location ())
139- yesterday := midnight .Add (- 1 * time .Second )
140- today := midnight .Add (1 * time .Second )
141- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "yesterday.txt" ), []byte ("yesterday" ), 0o600 ))
142- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "yesterday.txt" ), yesterday , yesterday ))
143- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "today.txt" ), []byte ("today" ), 0o600 ))
144- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "today.txt" ), today , today ))
145-
146- // 2. Prepare files for thisweek vs lastweek
147- // Monday = 1 ... Sunday = 7 (in Go Sunday is 0)
148- // We want offset from Monday. 0=Monday, ..., 6=Sunday
149- offset := (int (now .Weekday ()) + 6 ) % 7
150- startOfWeek := now .AddDate (0 , 0 , - offset )
151- startOfWeekMidnight := time .Date (startOfWeek .Year (), startOfWeek .Month (), startOfWeek .Day (), 0 , 0 , 0 , 0 , now .Location ())
152- lastWeek := startOfWeekMidnight .Add (- 1 * time .Second )
153- thisWeek := startOfWeekMidnight .Add (1 * time .Second )
154- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "lastweek.txt" ), []byte ("lastweek" ), 0o600 ))
155- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "lastweek.txt" ), lastWeek , lastWeek ))
156- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "thisweek.txt" ), []byte ("thisweek" ), 0o600 ))
157- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "thisweek.txt" ), thisWeek , thisWeek ))
158-
159- // 3. Prepare files for thismonth vs lastmonth
160- startOfMonthMidnight := time .Date (now .Year (), now .Month (), 1 , 0 , 0 , 0 , 0 , now .Location ())
161- lastMonth := startOfMonthMidnight .Add (- 1 * time .Second )
162- thisMonth := startOfMonthMidnight .Add (1 * time .Second )
163- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "lastmonth.txt" ), []byte ("lastmonth" ), 0o600 ))
164- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "lastmonth.txt" ), lastMonth , lastMonth ))
165- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "thismonth.txt" ), []byte ("thismonth" ), 0o600 ))
166- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "thismonth.txt" ), thisMonth , thisMonth ))
167-
168- // 4. Prepare files for thisyear vs lastyear
169- startOfYearMidnight := time .Date (now .Year (), time .January , 1 , 0 , 0 , 0 , 0 , now .Location ())
170- lastYear := startOfYearMidnight .Add (- 1 * time .Second )
171- thisYear := startOfYearMidnight .Add (1 * time .Second )
172- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "lastyear.txt" ), []byte ("lastyear" ), 0o600 ))
173- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "lastyear.txt" ), lastYear , lastYear ))
174- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "thisyear.txt" ), []byte ("thisyear" ), 0o600 ))
175- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "thisyear.txt" ), thisYear , thisYear ))
136+ prepareDateKeywordFiles (t , tmpPath , false )
176137
177138 tests := []struct {
178139 keyword string
@@ -196,44 +157,7 @@ func TestCheckFilesFilterDateKeywordsUTC(t *testing.T) {
196157 snc := StartTestAgent (t , "" )
197158 tmpPath := t .TempDir ()
198159
199- // 1. Prepare files for today:utc vs yesterday
200- now := time .Now ().UTC ()
201- midnight := time .Date (now .Year (), now .Month (), now .Day (), 0 , 0 , 0 , 0 , time .UTC )
202- yesterday := midnight .Add (- 1 * time .Second )
203- today := midnight .Add (1 * time .Second )
204- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "yesterday_utc.txt" ), []byte ("yesterday_utc" ), 0o600 ))
205- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "yesterday_utc.txt" ), yesterday , yesterday ))
206- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "today_utc.txt" ), []byte ("today_utc" ), 0o600 ))
207- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "today_utc.txt" ), today , today ))
208-
209- // 2. Prepare files for thisweek:utc vs lastweek
210- offset := (int (now .Weekday ()) + 6 ) % 7
211- startOfWeek := now .AddDate (0 , 0 , - offset )
212- startOfWeekMidnight := time .Date (startOfWeek .Year (), startOfWeek .Month (), startOfWeek .Day (), 0 , 0 , 0 , 0 , time .UTC )
213- lastWeek := startOfWeekMidnight .Add (- 1 * time .Second )
214- thisWeek := startOfWeekMidnight .Add (1 * time .Second )
215- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "lastweek_utc.txt" ), []byte ("lastweek_utc" ), 0o600 ))
216- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "lastweek_utc.txt" ), lastWeek , lastWeek ))
217- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "thisweek_utc.txt" ), []byte ("thisweek_utc" ), 0o600 ))
218- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "thisweek_utc.txt" ), thisWeek , thisWeek ))
219-
220- // 3. Prepare files for thismonth:utc vs lastmonth
221- startOfMonthMidnight := time .Date (now .Year (), now .Month (), 1 , 0 , 0 , 0 , 0 , time .UTC )
222- lastMonth := startOfMonthMidnight .Add (- 1 * time .Second )
223- thisMonth := startOfMonthMidnight .Add (1 * time .Second )
224- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "lastmonth_utc.txt" ), []byte ("lastmonth_utc" ), 0o600 ))
225- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "lastmonth_utc.txt" ), lastMonth , lastMonth ))
226- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "thismonth_utc.txt" ), []byte ("thismonth_utc" ), 0o600 ))
227- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "thismonth_utc.txt" ), thisMonth , thisMonth ))
228-
229- // 4. Prepare files for thisyear:utc vs lastyear
230- startOfYearMidnight := time .Date (now .Year (), time .January , 1 , 0 , 0 , 0 , 0 , time .UTC )
231- lastYear := startOfYearMidnight .Add (- 1 * time .Second )
232- thisYear := startOfYearMidnight .Add (1 * time .Second )
233- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "lastyear_utc.txt" ), []byte ("lastyear_utc" ), 0o600 ))
234- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "lastyear_utc.txt" ), lastYear , lastYear ))
235- require .NoError (t , os .WriteFile (filepath .Join (tmpPath , "thisyear_utc.txt" ), []byte ("thisyear_utc" ), 0o600 ))
236- require .NoError (t , os .Chtimes (filepath .Join (tmpPath , "thisyear_utc.txt" ), thisYear , thisYear ))
160+ prepareDateKeywordFiles (t , tmpPath , true )
237161
238162 tests := []struct {
239163 keyword string
@@ -253,6 +177,57 @@ func TestCheckFilesFilterDateKeywordsUTC(t *testing.T) {
253177 StopTestAgent (t , snc )
254178}
255179
180+ func prepareDateKeywordFiles (t * testing.T , tmpPath string , utc bool ) {
181+ t .Helper ()
182+ suffix := ".txt"
183+ if utc {
184+ suffix = "_utc.txt"
185+ }
186+
187+ now := time .Now ()
188+ if utc {
189+ now = now .UTC ()
190+ }
191+ location := now .Location ()
192+
193+ // 1. Prepare files for today vs yesterday
194+ midnight := time .Date (now .Year (), now .Month (), now .Day (), 0 , 0 , 0 , 0 , location )
195+ yesterday := midnight .Add (- 1 * time .Second )
196+ today := midnight .Add (1 * time .Second )
197+ createTestFile (t , tmpPath , "yesterday" + suffix , yesterday )
198+ createTestFile (t , tmpPath , "today" + suffix , today )
199+
200+ // 2. Prepare files for thisweek vs lastweek
201+ offset := (int (now .Weekday ()) + 6 ) % 7
202+ startOfWeek := now .AddDate (0 , 0 , - offset )
203+ startOfWeekMidnight := time .Date (startOfWeek .Year (), startOfWeek .Month (), startOfWeek .Day (), 0 , 0 , 0 , 0 , location )
204+ lastWeek := startOfWeekMidnight .Add (- 1 * time .Second )
205+ thisWeek := startOfWeekMidnight .Add (1 * time .Second )
206+ createTestFile (t , tmpPath , "lastweek" + suffix , lastWeek )
207+ createTestFile (t , tmpPath , "thisweek" + suffix , thisWeek )
208+
209+ // 3. Prepare files for thismonth vs lastmonth
210+ startOfMonthMidnight := time .Date (now .Year (), now .Month (), 1 , 0 , 0 , 0 , 0 , location )
211+ lastMonth := startOfMonthMidnight .Add (- 1 * time .Second )
212+ thisMonth := startOfMonthMidnight .Add (1 * time .Second )
213+ createTestFile (t , tmpPath , "lastmonth" + suffix , lastMonth )
214+ createTestFile (t , tmpPath , "thismonth" + suffix , thisMonth )
215+
216+ // 4. Prepare files for thisyear vs lastyear
217+ startOfYearMidnight := time .Date (now .Year (), time .January , 1 , 0 , 0 , 0 , 0 , location )
218+ lastYear := startOfYearMidnight .Add (- 1 * time .Second )
219+ thisYear := startOfYearMidnight .Add (1 * time .Second )
220+ createTestFile (t , tmpPath , "lastyear" + suffix , lastYear )
221+ createTestFile (t , tmpPath , "thisyear" + suffix , thisYear )
222+ }
223+
224+ func createTestFile (t * testing.T , dir , name string , modTime time.Time ) {
225+ t .Helper ()
226+ p := filepath .Join (dir , name )
227+ require .NoError (t , os .WriteFile (p , []byte (name ), 0o600 ))
228+ require .NoError (t , os .Chtimes (p , modTime , modTime ))
229+ }
230+
256231func verifyCheckFilesDateKeyword (t * testing.T , snc * Agent , tmpPath , keyword , older , newer string ) {
257232 t .Helper ()
258233 t .Run (keyword , func (t * testing.T ) {
0 commit comments