@@ -145,4 +145,65 @@ impl UpdateConfig {
145145 pub fn is_version_skipped ( & self , version : & str ) -> bool {
146146 self . skip_version . as_deref ( ) == Some ( version)
147147 }
148+
149+ /// Check if this version has already been surfaced to the user.
150+ pub fn is_version_notified ( & self , version : & str ) -> bool {
151+ self . last_notified_version . as_deref ( ) == Some ( version)
152+ }
153+
154+ /// Persist the latest version that has already been surfaced to the user.
155+ pub fn mark_version_notified ( & mut self , version : & str ) -> Result < ( ) , std:: io:: Error > {
156+ if self . is_version_notified ( version) {
157+ return Ok ( ( ) ) ;
158+ }
159+
160+ self . last_notified_version = Some ( version. to_string ( ) ) ;
161+ self . save ( )
162+ }
163+ }
164+
165+ #[ cfg( test) ]
166+ mod tests {
167+ use super :: * ;
168+ use std:: ffi:: OsString ;
169+ use std:: sync:: Mutex ;
170+
171+ static HOME_LOCK : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
172+
173+ struct HomeGuard ( Option < OsString > ) ;
174+
175+ impl Drop for HomeGuard {
176+ fn drop ( & mut self ) {
177+ if let Some ( original) = self . 0 . take ( ) {
178+ unsafe { std:: env:: set_var ( "HOME" , original) } ;
179+ } else {
180+ unsafe { std:: env:: remove_var ( "HOME" ) } ;
181+ }
182+ }
183+ }
184+
185+ #[ test]
186+ fn test_is_version_notified ( ) {
187+ let config = UpdateConfig {
188+ last_notified_version : Some ( "0.9.0" . to_string ( ) ) ,
189+ ..UpdateConfig :: default ( )
190+ } ;
191+
192+ assert ! ( config. is_version_notified( "0.9.0" ) ) ;
193+ assert ! ( !config. is_version_notified( "0.9.1" ) ) ;
194+ }
195+
196+ #[ test]
197+ fn test_mark_version_notified_persists ( ) {
198+ let _lock = HOME_LOCK . lock ( ) . unwrap ( ) ;
199+ let _guard = HomeGuard ( std:: env:: var_os ( "HOME" ) ) ;
200+ let temp_home = tempfile:: tempdir ( ) . unwrap ( ) ;
201+ unsafe { std:: env:: set_var ( "HOME" , temp_home. path ( ) ) } ;
202+
203+ let mut config = UpdateConfig :: default ( ) ;
204+ config. mark_version_notified ( "1.2.3" ) . unwrap ( ) ;
205+
206+ let loaded = UpdateConfig :: load ( ) ;
207+ assert_eq ! ( loaded. last_notified_version. as_deref( ) , Some ( "1.2.3" ) ) ;
208+ }
148209}
0 commit comments