11use std:: env:: temp_dir;
2- use std:: fs:: File ;
3- use std:: fs:: { remove_file, rename} ;
4- use std:: io:: Write ;
2+ use std:: fs:: rename;
53use std:: path:: Path ;
64
75use chrono:: Local ;
86
97use crate :: auth_file_watcher:: AuthFileWatcher ;
8+ use crate :: test_utils:: test_file:: TestFile ;
109
1110const AUTH_FAILED_MESSAGES : [ & str ; 6 ] = [
1211 "workstation sudo: pam_unix(sudo:auth): authentication failure; logname=john uid=1000 euid=0 tty=/dev/pts/7 ruser=john rhost= user=john" ,
@@ -17,54 +16,6 @@ const AUTH_FAILED_MESSAGES: [&str; 6] = [
1716 "workstation PackageKit: uid 1000 is trying to obtain org.freedesktop.packagekit.system-sources-refresh auth (only_trusted:0)" ,
1817] ;
1918
20- struct TestFile {
21- pub filepath : String ,
22- file : File ,
23- }
24-
25- impl TestFile {
26- pub fn new ( prefix : & str ) -> TestFile {
27- let filename = format ! ( "{}-{}.log" , prefix, Local :: now( ) . timestamp_micros( ) ) ;
28- let filepath_buffer = temp_dir ( ) . join ( filename) ;
29- let filepath = filepath_buffer. to_str ( ) . expect ( "Error creating filepath" ) ;
30- println ! ( "Creating test file: {}" , filepath) ;
31- return TestFile {
32- filepath : String :: from ( filepath) ,
33- file : File :: create ( filepath) . expect ( "Error creating test file" ) ,
34- } ;
35- }
36-
37- pub fn create ( & mut self ) {
38- println ! ( "Creating test file: {}" , & self . filepath) ;
39- self . file = File :: create ( & self . filepath ) . expect ( "Error creating test file" ) ;
40- }
41-
42- pub fn write ( & mut self , message : & str ) {
43- let bytes_to_add = message. as_bytes ( ) ;
44- let bytes_written = self
45- . file
46- . write ( bytes_to_add)
47- . expect ( "Error writing to file" ) ;
48- assert_eq ! ( bytes_written, bytes_to_add. len( ) ) ;
49- }
50-
51- pub fn truncate ( & mut self ) {
52- println ! ( "Truncating test file: {}" , self . filepath) ;
53- self . file . set_len ( 0 ) . expect ( "Error truncating file" ) ;
54- }
55-
56- pub fn remove ( & mut self ) {
57- println ! ( "Removing test file: {}" , self . filepath) ;
58- remove_file ( & self . filepath ) . expect ( "Unable to remove test file" ) ;
59- }
60- }
61-
62- impl Drop for TestFile {
63- fn drop ( & mut self ) {
64- self . remove ( ) ;
65- }
66- }
67-
6819#[ test]
6920fn when_monitored_file_does_not_exist_then_new_does_not_return_error ( ) {
7021 let filepath_buffer = temp_dir ( ) . join ( "auth-monitor-non-existing-file.log" ) ;
0 commit comments