• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ortham / libloadorder / 13001309526

28 Jan 2025 12:47AM UTC coverage: 92.626% (+0.07%) from 92.56%
13001309526

push

github

Ortham
Set versions and changelogs for 18.2.0

9295 of 10035 relevant lines covered (92.63%)

1443533.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.14
/src/plugin.rs
1
use std::ffi::OsStr;
2
/*
3
 * This file is part of libloadorder
4
 *
5
 * Copyright (C) 2017 Oliver Hamlet
6
 *
7
 * libloadorder is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * libloadorder is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with libloadorder. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
use std::fs::{File, FileTimes};
21
use std::path::Path;
22
use std::time::SystemTime;
23

24
use esplugin::ParseOptions;
25
use unicase::eq;
26

27
use crate::enums::{Error, GameId};
28
use crate::game_settings::GameSettings;
29

30
const VALID_EXTENSIONS: &[&str] = &[".esp", ".esm", ".esp.ghost", ".esm.ghost"];
31

32
const VALID_EXTENSIONS_WITH_ESL: &[&str] = &[
33
    ".esp",
34
    ".esm",
35
    ".esp.ghost",
36
    ".esm.ghost",
37
    ".esl",
38
    ".esl.ghost",
39
];
40

41
const VALID_EXTENSIONS_OPENMW: &[&str] = &[".esp", ".esm", ".omwaddon", ".omwgame", ".omwscripts"];
42

43
#[derive(Clone, Debug)]
44
pub struct Plugin {
45
    active: bool,
46
    modification_time: SystemTime,
47
    data: esplugin::Plugin,
48
    name: String,
49
    game_id: GameId,
50
}
51

52
impl Plugin {
53
    pub fn new(filename: &str, game_settings: &GameSettings) -> Result<Plugin, Error> {
20,156✔
54
        Plugin::with_active(filename, game_settings, false)
20,156✔
55
    }
20,156✔
56

57
    pub fn with_active(
20,757✔
58
        filename: &str,
20,757✔
59
        game_settings: &GameSettings,
20,757✔
60
        active: bool,
20,757✔
61
    ) -> Result<Plugin, Error> {
20,757✔
62
        let filepath = game_settings.plugin_path(filename);
20,757✔
63

64
        let filepath = if game_settings.id().allow_plugin_ghosting() {
20,757✔
65
            use crate::ghostable_path::GhostablePath;
66

67
            if active {
20,682✔
68
                filepath.unghost()?
366✔
69
            } else {
70
                filepath.resolve_path()?
20,316✔
71
            }
72
        } else {
73
            filepath
75✔
74
        };
75

76
        Plugin::with_path(&filepath, game_settings.id(), active)
20,751✔
77
    }
20,757✔
78

79
    pub(crate) fn with_path(path: &Path, game_id: GameId, active: bool) -> Result<Plugin, Error> {
20,770✔
80
        let filename = match path.file_name().and_then(OsStr::to_str) {
20,770✔
81
            Some(n) => n,
20,770✔
82
            None => return Err(Error::NoFilename(path.to_path_buf())),
×
83
        };
84

85
        if !has_plugin_extension(filename, game_id) {
20,770✔
86
            return Err(Error::InvalidPath(path.to_path_buf()));
1✔
87
        }
20,769✔
88

89
        let file = File::open(path).map_err(|e| Error::IoError(path.to_path_buf(), e))?;
20,769✔
90
        let modification_time = file
20,649✔
91
            .metadata()
20,649✔
92
            .and_then(|m| m.modified())
20,649✔
93
            .map_err(|e| Error::IoError(path.to_path_buf(), e))?;
20,649✔
94

95
        let mut data = esplugin::Plugin::new(game_id.to_esplugin_id(), path);
20,649✔
96

20,649✔
97
        // OpenMW has .omwscripts plugins that form part of the load order but
20,649✔
98
        // are not of the same file format as the .esm/.esp/.omwgame/.omwaddon
20,649✔
99
        // files.
20,649✔
100
        if !iends_with_ascii(filename, ".omwscripts") {
20,649✔
101
            data.parse_reader(file, ParseOptions::header_only())
20,642✔
102
                .map_err(|e| file_error(path, e))?;
20,642✔
103
        }
7✔
104

105
        Ok(Plugin {
20,643✔
106
            active,
20,643✔
107
            modification_time,
20,643✔
108
            data,
20,643✔
109
            name: trim_dot_ghost(filename, game_id).to_string(),
20,643✔
110
            game_id,
20,643✔
111
        })
20,643✔
112
    }
20,770✔
113

114
    pub fn name(&self) -> &str {
377,544,511✔
115
        &self.name
377,544,511✔
116
    }
377,544,511✔
117

118
    pub fn name_matches(&self, string: &str) -> bool {
377,366,726✔
119
        eq(self.name(), trim_dot_ghost(string, self.game_id))
377,366,726✔
120
    }
377,366,726✔
121

122
    pub fn modification_time(&self) -> SystemTime {
178✔
123
        self.modification_time
178✔
124
    }
178✔
125

126
    pub fn is_active(&self) -> bool {
528,100✔
127
        self.active
528,100✔
128
    }
528,100✔
129

130
    pub fn is_master_file(&self) -> bool {
86,904,741✔
131
        self.game_id != GameId::OpenMW && self.data.is_master_file()
86,904,741✔
132
    }
86,904,741✔
133

134
    pub fn is_light_plugin(&self) -> bool {
273,518✔
135
        self.data.is_light_plugin()
273,518✔
136
    }
273,518✔
137

138
    pub fn is_medium_plugin(&self) -> bool {
236,644✔
139
        self.data.is_medium_plugin()
236,644✔
140
    }
236,644✔
141

142
    pub fn is_blueprint_master(&self) -> bool {
694,057,032✔
143
        self.data.is_blueprint_plugin() && self.is_master_file()
694,057,032✔
144
    }
694,057,032✔
145

146
    pub fn masters(&self) -> Result<Vec<String>, Error> {
43,422,663✔
147
        self.data
43,422,663✔
148
            .masters()
43,422,663✔
149
            .map_err(|e| file_error(self.data.path(), e))
43,422,663✔
150
    }
43,422,663✔
151

152
    pub fn set_modification_time(&mut self, time: SystemTime) -> Result<(), Error> {
39✔
153
        // Always write the file time. This has a huge performance impact, but
39✔
154
        // is important for correctness, as otherwise external changes to plugin
39✔
155
        // timestamps between calls to WritableLoadOrder::load() and
39✔
156
        // WritableLoadOrder::save() could lead to libloadorder not setting all
39✔
157
        // the timestamps it needs to and producing an incorrect load order.
39✔
158
        let times = FileTimes::new()
39✔
159
            .set_accessed(SystemTime::now())
39✔
160
            .set_modified(time);
39✔
161

39✔
162
        File::options()
39✔
163
            .write(true)
39✔
164
            .open(self.data.path())
39✔
165
            .and_then(|f| f.set_times(times))
39✔
166
            .map_err(|e| Error::IoError(self.data.path().to_path_buf(), e))?;
39✔
167

168
        self.modification_time = time;
39✔
169
        Ok(())
39✔
170
    }
39✔
171

172
    pub fn activate(&mut self) -> Result<(), Error> {
11,820✔
173
        if !self.is_active() {
11,820✔
174
            if self.game_id.allow_plugin_ghosting() {
11,819✔
175
                use crate::ghostable_path::GhostablePath;
176

177
                if self.data.path().has_ghost_extension() {
11,813✔
178
                    let new_path = self.data.path().unghost()?;
1✔
179

180
                    self.data = esplugin::Plugin::new(self.data.game_id(), &new_path);
1✔
181
                    self.data
1✔
182
                        .parse_file(ParseOptions::header_only())
1✔
183
                        .map_err(|e| file_error(self.data.path(), e))?;
1✔
184
                    let modification_time = self.modification_time();
1✔
185
                    self.set_modification_time(modification_time)?;
1✔
186
                }
11,812✔
187
            }
6✔
188

189
            self.active = true;
11,819✔
190
        }
1✔
191
        Ok(())
11,820✔
192
    }
11,820✔
193

194
    pub fn deactivate(&mut self) {
11,963✔
195
        self.active = false;
11,963✔
196
    }
11,963✔
197
}
198

199
pub fn has_plugin_extension(filename: &str, game: GameId) -> bool {
21,199✔
200
    let valid_extensions = if game == GameId::OpenMW {
21,199✔
201
        VALID_EXTENSIONS_OPENMW
123✔
202
    } else if game.supports_light_plugins() {
21,076✔
203
        VALID_EXTENSIONS_WITH_ESL
19,672✔
204
    } else {
205
        VALID_EXTENSIONS
1,404✔
206
    };
207

208
    valid_extensions
21,199✔
209
        .iter()
21,199✔
210
        .any(|e| iends_with_ascii(filename, e))
41,619✔
211
}
21,199✔
212

213
fn iends_with_ascii(string: &str, suffix: &str) -> bool {
377,449,949✔
214
    // as_bytes().into_iter() is faster than bytes().
377,449,949✔
215
    string.len() >= suffix.len()
377,449,949✔
216
        && string
377,449,391✔
217
            .as_bytes()
377,449,391✔
218
            .iter()
377,449,391✔
219
            .rev()
377,449,391✔
220
            .zip(suffix.as_bytes().iter().rev())
377,449,391✔
221
            .all(|(string_byte, suffix_byte)| string_byte.eq_ignore_ascii_case(suffix_byte))
377,513,706✔
222
}
377,449,949✔
223

224
pub fn trim_dot_ghost(string: &str, game_id: GameId) -> &str {
377,387,779✔
225
    if game_id.allow_plugin_ghosting() {
377,387,779✔
226
        trim_dot_ghost_unchecked(string)
377,387,571✔
227
    } else {
228
        string
208✔
229
    }
230
}
377,387,779✔
231

232
pub fn trim_dot_ghost_unchecked(string: &str) -> &str {
377,387,681✔
233
    use crate::ghostable_path::GHOST_FILE_EXTENSION;
234

235
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
377,387,681✔
236
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
23✔
237
    } else {
238
        string
377,387,658✔
239
    }
240
}
377,387,681✔
241

242
fn file_error(file_path: &Path, error: esplugin::Error) -> Error {
6✔
243
    match error {
6✔
244
        esplugin::Error::IoError(x) => Error::IoError(file_path.to_path_buf(), x),
6✔
245
        esplugin::Error::NoFilename(_) => Error::NoFilename(file_path.to_path_buf()),
×
246
        e => Error::PluginParsingError(file_path.to_path_buf(), Box::new(e)),
×
247
    }
248
}
6✔
249

250
#[cfg(test)]
251
mod tests {
252
    use super::*;
253

254
    use crate::tests::{copy_to_test_dir, openmw_settings};
255
    use std::path::{Path, PathBuf};
256
    use std::time::{Duration, UNIX_EPOCH};
257
    use tempfile::tempdir;
258

259
    fn game_settings(game_id: GameId, game_path: &Path) -> GameSettings {
14✔
260
        if game_id == GameId::OpenMW {
14✔
261
            std::fs::create_dir(game_path.join("Data Files")).unwrap();
×
262
        }
14✔
263

264
        GameSettings::with_local_and_my_games_paths(
14✔
265
            game_id,
14✔
266
            game_path,
14✔
267
            &PathBuf::default(),
14✔
268
            PathBuf::default(),
14✔
269
        )
14✔
270
        .unwrap()
14✔
271
    }
14✔
272

273
    #[test]
274
    fn with_active_should_unghost_active_ghosted_plugin_paths() {
1✔
275
        let tmp_dir = tempdir().unwrap();
1✔
276
        let game_dir = tmp_dir.path();
1✔
277

1✔
278
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
279

1✔
280
        let name = "Blank.esp";
1✔
281
        let ghosted_name = "Blank.esp.ghost";
1✔
282

1✔
283
        copy_to_test_dir(name, ghosted_name, &settings);
1✔
284
        let plugin = Plugin::with_active(ghosted_name, &settings, true).unwrap();
1✔
285

1✔
286
        assert_eq!(name, plugin.name());
1✔
287
        assert!(game_dir.join("Data").join(name).exists());
1✔
288
        assert!(!game_dir.join("Data").join(ghosted_name).exists());
1✔
289
    }
1✔
290

291
    #[test]
292
    fn with_active_should_resolve_inactive_ghosted_plugin_paths() {
1✔
293
        let tmp_dir = tempdir().unwrap();
1✔
294
        let game_dir = tmp_dir.path();
1✔
295

1✔
296
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
297

1✔
298
        let name = "Blank.esp";
1✔
299
        let ghosted_name = "Blank.esp.ghost";
1✔
300

1✔
301
        copy_to_test_dir(name, ghosted_name, &settings);
1✔
302
        let plugin = Plugin::with_active(ghosted_name, &settings, false).unwrap();
1✔
303

1✔
304
        assert_eq!(name, plugin.name());
1✔
305
        assert!(!game_dir.join("Data").join(name).exists());
1✔
306
        assert!(game_dir.join("Data").join(ghosted_name).exists());
1✔
307
    }
1✔
308

309
    #[test]
310
    fn with_active_should_not_resolve_ghosted_plugin_paths_for_openmw() {
1✔
311
        let tmp_dir = tempdir().unwrap();
1✔
312
        let tmp_path = tmp_dir.path();
1✔
313
        let game_path = tmp_path.join("game");
1✔
314

1✔
315
        let settings = openmw_settings(tmp_path);
1✔
316

1✔
317
        let name = "Blank.esp";
1✔
318
        let ghosted_name = "Blank.esp.ghost";
1✔
319

1✔
320
        copy_to_test_dir(name, ghosted_name, &settings);
1✔
321
        match Plugin::with_active(ghosted_name, &settings, false).unwrap_err() {
1✔
322
            Error::InvalidPath(p) => {
1✔
323
                assert_eq!(game_path.join("resources/vfs").join(ghosted_name), p)
1✔
324
            }
325
            e => panic!("Expected invalid path error, got {:?}", e),
×
326
        }
327
    }
1✔
328

329
    #[test]
330
    fn name_should_return_the_plugin_filename_without_any_ghost_extension() {
1✔
331
        let tmp_dir = tempdir().unwrap();
1✔
332
        let game_dir = tmp_dir.path();
1✔
333

1✔
334
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
335

1✔
336
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
337
        let plugin = Plugin::new("Blank.esp.ghost", &settings).unwrap();
1✔
338
        assert_eq!("Blank.esp", plugin.name());
1✔
339

340
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
341
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
342
        assert_eq!("Blank.esp", plugin.name());
1✔
343

344
        copy_to_test_dir("Blank.esm", "Blank.esm.ghost", &settings);
1✔
345
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
346
        assert_eq!("Blank.esm", plugin.name());
1✔
347
    }
1✔
348

349
    #[test]
350
    fn name_matches_should_ignore_plugin_ghost_extension() {
1✔
351
        let tmp_dir = tempdir().unwrap();
1✔
352
        let settings = game_settings(GameId::Skyrim, tmp_dir.path());
1✔
353
        copy_to_test_dir("Blank.esp", "BlanK.esp.GHoSt", &settings);
1✔
354

1✔
355
        let plugin = Plugin::new("BlanK.esp.GHoSt", &settings).unwrap();
1✔
356
        assert!(plugin.name_matches("Blank.esp"));
1✔
357
    }
1✔
358

359
    #[test]
360
    fn name_matches_should_ignore_string_ghost_suffix() {
1✔
361
        let tmp_dir = tempdir().unwrap();
1✔
362
        let settings = game_settings(GameId::Skyrim, tmp_dir.path());
1✔
363
        copy_to_test_dir("Blank.esp", "BlanK.esp", &settings);
1✔
364

1✔
365
        let plugin = Plugin::new("BlanK.esp", &settings).unwrap();
1✔
366
        assert!(plugin.name_matches("Blank.esp.GHoSt"));
1✔
367
    }
1✔
368

369
    #[test]
370
    fn modification_time_should_return_the_plugin_modification_time_at_creation() {
1✔
371
        let tmp_dir = tempdir().unwrap();
1✔
372
        let game_dir = tmp_dir.path();
1✔
373

1✔
374
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
375

1✔
376
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
377
        let plugin_path = game_dir.join("Data").join("Blank.esp");
1✔
378
        let mtime = plugin_path.metadata().unwrap().modified().unwrap();
1✔
379

1✔
380
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
381
        assert_eq!(mtime, plugin.modification_time());
1✔
382
    }
1✔
383

384
    #[test]
385
    fn is_active_should_be_false() {
1✔
386
        let tmp_dir = tempdir().unwrap();
1✔
387
        let game_dir = tmp_dir.path();
1✔
388

1✔
389
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
390

1✔
391
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
392
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
393

1✔
394
        assert!(!plugin.is_active());
1✔
395
    }
1✔
396

397
    #[test]
398
    fn is_master_file_should_be_true_if_the_plugin_is_a_master_file() {
1✔
399
        let tmp_dir = tempdir().unwrap();
1✔
400
        let game_dir = tmp_dir.path();
1✔
401

1✔
402
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
403

1✔
404
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
405
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
406

1✔
407
        assert!(plugin.is_master_file());
1✔
408
    }
1✔
409

410
    #[test]
411
    fn is_master_file_should_be_false_if_the_plugin_is_not_a_master_file() {
1✔
412
        let tmp_dir = tempdir().unwrap();
1✔
413
        let game_dir = tmp_dir.path();
1✔
414

1✔
415
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
416

1✔
417
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
418
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
419

1✔
420
        assert!(!plugin.is_master_file());
1✔
421
    }
1✔
422

423
    #[test]
424
    fn is_master_file_should_be_false_for_all_openmw_plugins() {
1✔
425
        let tmp_dir = tempdir().unwrap();
1✔
426
        let game_dir = tmp_dir.path();
1✔
427

1✔
428
        let settings = openmw_settings(game_dir);
1✔
429

1✔
430
        let name = "plugin.omwscripts";
1✔
431
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
432
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
433

1✔
434
        assert!(!plugin.is_master_file());
1✔
435

436
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
437
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
438

1✔
439
        assert!(!plugin.is_master_file());
1✔
440

441
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
442
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
443

1✔
444
        assert!(!plugin.is_master_file());
1✔
445
    }
1✔
446

447
    #[test]
448
    fn is_light_plugin_should_be_true_for_esl_files_only() {
1✔
449
        let tmp_dir = tempdir().unwrap();
1✔
450
        let game_dir = tmp_dir.path();
1✔
451

1✔
452
        let settings = game_settings(GameId::SkyrimSE, game_dir);
1✔
453

1✔
454
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
455
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
456

1✔
457
        assert!(!plugin.is_master_file());
1✔
458

459
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
460
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
461

1✔
462
        assert!(!plugin.is_light_plugin());
1✔
463

464
        copy_to_test_dir("Blank.esm", "Blank.esl", &settings);
1✔
465
        let plugin = Plugin::new("Blank.esl", &settings).unwrap();
1✔
466

1✔
467
        assert!(plugin.is_light_plugin());
1✔
468

469
        copy_to_test_dir("Blank - Different.esp", "Blank - Different.esl", &settings);
1✔
470
        let plugin = Plugin::new("Blank - Different.esl", &settings).unwrap();
1✔
471

1✔
472
        assert!(plugin.is_light_plugin());
1✔
473
    }
1✔
474

475
    #[test]
476
    fn is_light_plugin_should_be_false_for_an_omwscripts_plugin() {
1✔
477
        let tmp_dir = tempdir().unwrap();
1✔
478
        let game_dir = tmp_dir.path();
1✔
479

1✔
480
        let settings = openmw_settings(game_dir);
1✔
481

1✔
482
        let name = "plugin.omwscripts";
1✔
483
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
484
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
485

1✔
486
        assert!(!plugin.is_light_plugin());
1✔
487
    }
1✔
488

489
    #[test]
490
    fn is_medium_plugin_should_be_false_for_an_omwscripts_plugin() {
1✔
491
        let tmp_dir = tempdir().unwrap();
1✔
492
        let game_dir = tmp_dir.path();
1✔
493

1✔
494
        let settings = openmw_settings(game_dir);
1✔
495

1✔
496
        let name = "plugin.omwscripts";
1✔
497
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
498
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
499

1✔
500
        assert!(!plugin.is_light_plugin());
1✔
501
    }
1✔
502

503
    #[test]
504
    fn is_blueprint_master_should_be_false_for_an_omwscripts_plugin() {
1✔
505
        let tmp_dir = tempdir().unwrap();
1✔
506
        let game_dir = tmp_dir.path();
1✔
507

1✔
508
        let settings = openmw_settings(game_dir);
1✔
509

1✔
510
        let name = "plugin.omwscripts";
1✔
511
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
512
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
513

1✔
514
        assert!(!plugin.is_blueprint_master());
1✔
515
    }
1✔
516

517
    #[test]
518
    fn masters_should_be_empty_for_an_omwscripts_plugin() {
1✔
519
        let tmp_dir = tempdir().unwrap();
1✔
520
        let game_dir = tmp_dir.path();
1✔
521

1✔
522
        let settings = openmw_settings(game_dir);
1✔
523

1✔
524
        let name = "plugin.omwscripts";
1✔
525
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
526
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
527

1✔
528
        assert!(plugin.masters().unwrap().is_empty());
1✔
529
    }
1✔
530

531
    #[test]
532
    fn set_modification_time_should_update_the_file_modification_time() {
1✔
533
        let tmp_dir = tempdir().unwrap();
1✔
534
        let game_dir = tmp_dir.path();
1✔
535

1✔
536
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
537

1✔
538
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
539

1✔
540
        let path = game_dir.join("Data").join("Blank.esp");
1✔
541
        let file_size = path.metadata().unwrap().len();
1✔
542

1✔
543
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
544

1✔
545
        assert_ne!(UNIX_EPOCH, plugin.modification_time());
1✔
546
        plugin.set_modification_time(UNIX_EPOCH).unwrap();
1✔
547

1✔
548
        let metadata = path.metadata().unwrap();
1✔
549
        let new_mtime = metadata.modified().unwrap();
1✔
550
        let new_size = metadata.len();
1✔
551

1✔
552
        assert_eq!(UNIX_EPOCH, plugin.modification_time());
1✔
553
        assert_eq!(UNIX_EPOCH, new_mtime);
1✔
554
        assert_eq!(file_size, new_size);
1✔
555
    }
1✔
556

557
    #[test]
558
    fn set_modification_time_should_be_able_to_handle_pre_unix_timestamps() {
1✔
559
        let tmp_dir = tempdir().unwrap();
1✔
560
        let game_dir = tmp_dir.path();
1✔
561

1✔
562
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
563

1✔
564
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
565
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
566
        let target_mtime = UNIX_EPOCH - Duration::from_secs(1);
1✔
567

1✔
568
        assert_ne!(target_mtime, plugin.modification_time());
1✔
569
        plugin.set_modification_time(target_mtime).unwrap();
1✔
570
        let new_mtime = game_dir
1✔
571
            .join("Data")
1✔
572
            .join("Blank.esp")
1✔
573
            .metadata()
1✔
574
            .unwrap()
1✔
575
            .modified()
1✔
576
            .unwrap();
1✔
577

1✔
578
        assert_eq!(target_mtime, plugin.modification_time());
1✔
579
        assert_eq!(target_mtime, new_mtime);
1✔
580
    }
1✔
581

582
    #[test]
583
    fn activate_should_unghost_a_ghosted_plugin() {
1✔
584
        let tmp_dir = tempdir().unwrap();
1✔
585
        let game_dir = tmp_dir.path();
1✔
586

1✔
587
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
588

1✔
589
        copy_to_test_dir("Blank.esp", "Blank.esp.ghost", &settings);
1✔
590
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
591

1✔
592
        plugin.activate().unwrap();
1✔
593

1✔
594
        assert!(plugin.is_active());
1✔
595
        assert_eq!("Blank.esp", plugin.name());
1✔
596
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
597
    }
1✔
598

599
    #[test]
600
    fn activate_should_not_unghost_an_openmw_plugin() {
1✔
601
        // It's not possible to create an OpenMW Plugin from a path ending in
1✔
602
        // .ghost outside of this module, so this is just for internal
1✔
603
        // consistency.
1✔
604
        let tmp_dir = tempdir().unwrap();
1✔
605
        let game_dir = tmp_dir.path();
1✔
606

1✔
607
        let settings = openmw_settings(game_dir);
1✔
608

1✔
609
        let plugin_name = "Blank.esp.ghost";
1✔
610
        copy_to_test_dir("Blank.esp", plugin_name, &settings);
1✔
611

1✔
612
        let data = esplugin::Plugin::new(
1✔
613
            GameId::OpenMW.to_esplugin_id(),
1✔
614
            &game_dir.join("Data Files").join(plugin_name),
1✔
615
        );
1✔
616

1✔
617
        let mut plugin = Plugin {
1✔
618
            active: false,
1✔
619
            modification_time: SystemTime::now(),
1✔
620
            data,
1✔
621
            name: plugin_name.to_string(),
1✔
622
            game_id: GameId::OpenMW,
1✔
623
        };
1✔
624

1✔
625
        plugin.activate().unwrap();
1✔
626
        assert!(plugin.is_active());
1✔
627
        assert_eq!(plugin_name, plugin.name());
1✔
628
    }
1✔
629

630
    #[test]
631
    fn deactivate_should_not_ghost_a_plugin() {
1✔
632
        let tmp_dir = tempdir().unwrap();
1✔
633
        let game_dir = tmp_dir.path();
1✔
634

1✔
635
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
636

1✔
637
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
638
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
639

1✔
640
        plugin.deactivate();
1✔
641

1✔
642
        assert!(!plugin.is_active());
1✔
643
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
644
    }
1✔
645

646
    #[test]
647
    fn has_plugin_extension_should_recognise_openmw_extensions_for_openmw() {
1✔
648
        assert!(has_plugin_extension("plugin.omwgame", GameId::OpenMW));
1✔
649
        assert!(has_plugin_extension("plugin.omwaddon", GameId::OpenMW));
1✔
650
        assert!(has_plugin_extension("plugin.omwscripts", GameId::OpenMW));
1✔
651
    }
1✔
652

653
    #[test]
654
    fn has_plugin_extension_should_recognise_esp_and_esm_extensions_for_all_games() {
1✔
655
        assert!(has_plugin_extension("plugin.esp", GameId::OpenMW));
1✔
656
        assert!(has_plugin_extension("plugin.esp", GameId::Morrowind));
1✔
657
        assert!(has_plugin_extension("plugin.esp", GameId::Oblivion));
1✔
658
        assert!(has_plugin_extension("plugin.esp", GameId::Skyrim));
1✔
659
        assert!(has_plugin_extension("plugin.esp", GameId::SkyrimSE));
1✔
660
        assert!(has_plugin_extension("plugin.esp", GameId::SkyrimVR));
1✔
661
        assert!(has_plugin_extension("plugin.esp", GameId::Fallout3));
1✔
662
        assert!(has_plugin_extension("plugin.esp", GameId::FalloutNV));
1✔
663
        assert!(has_plugin_extension("plugin.esp", GameId::Fallout4));
1✔
664
        assert!(has_plugin_extension("plugin.esp", GameId::Fallout4VR));
1✔
665
        assert!(has_plugin_extension("plugin.esp", GameId::Starfield));
1✔
666

667
        assert!(has_plugin_extension("plugin.esm", GameId::OpenMW));
1✔
668
        assert!(has_plugin_extension("plugin.esm", GameId::Morrowind));
1✔
669
        assert!(has_plugin_extension("plugin.esm", GameId::Oblivion));
1✔
670
        assert!(has_plugin_extension("plugin.esm", GameId::Skyrim));
1✔
671
        assert!(has_plugin_extension("plugin.esm", GameId::SkyrimSE));
1✔
672
        assert!(has_plugin_extension("plugin.esm", GameId::SkyrimVR));
1✔
673
        assert!(has_plugin_extension("plugin.esm", GameId::Fallout3));
1✔
674
        assert!(has_plugin_extension("plugin.esm", GameId::FalloutNV));
1✔
675
        assert!(has_plugin_extension("plugin.esm", GameId::Fallout4));
1✔
676
        assert!(has_plugin_extension("plugin.esm", GameId::Fallout4VR));
1✔
677
        assert!(has_plugin_extension("plugin.esm", GameId::Starfield));
1✔
678
    }
1✔
679

680
    #[test]
681
    fn has_plugin_extension_should_recognise_ghosted_esp_and_esm_extensions_for_all_games_other_than_openmw(
1✔
682
    ) {
1✔
683
        assert!(!has_plugin_extension("plugin.esp.ghost", GameId::OpenMW));
1✔
684
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::Morrowind));
1✔
685
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::Oblivion));
1✔
686
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::Skyrim));
1✔
687
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::SkyrimSE));
1✔
688
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::SkyrimVR));
1✔
689
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::Fallout3));
1✔
690
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::FalloutNV));
1✔
691
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::Fallout4));
1✔
692
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::Fallout4VR));
1✔
693
        assert!(has_plugin_extension("plugin.esp.ghost", GameId::Starfield));
1✔
694

695
        assert!(!has_plugin_extension("plugin.esm.ghost", GameId::OpenMW));
1✔
696
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::Morrowind));
1✔
697
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::Oblivion));
1✔
698
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::Skyrim));
1✔
699
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::SkyrimSE));
1✔
700
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::SkyrimVR));
1✔
701
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::Fallout3));
1✔
702
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::FalloutNV));
1✔
703
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::Fallout4));
1✔
704
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::Fallout4VR));
1✔
705
        assert!(has_plugin_extension("plugin.esm.ghost", GameId::Starfield));
1✔
706
    }
1✔
707

708
    #[test]
709
    fn has_plugin_extension_should_recognise_esl_extension_and_ghosted_esl_for_fo4_and_later_games()
1✔
710
    {
1✔
711
        assert!(!has_plugin_extension("plugin.esl", GameId::OpenMW));
1✔
712
        assert!(!has_plugin_extension("plugin.esl", GameId::Morrowind));
1✔
713
        assert!(!has_plugin_extension("plugin.esl", GameId::Oblivion));
1✔
714
        assert!(!has_plugin_extension("plugin.esl", GameId::Skyrim));
1✔
715
        assert!(has_plugin_extension("plugin.esl", GameId::SkyrimSE));
1✔
716
        assert!(has_plugin_extension("plugin.esl", GameId::SkyrimVR));
1✔
717
        assert!(!has_plugin_extension("plugin.esl", GameId::Fallout3));
1✔
718
        assert!(!has_plugin_extension("plugin.esl", GameId::FalloutNV));
1✔
719
        assert!(has_plugin_extension("plugin.esl", GameId::Fallout4));
1✔
720
        assert!(has_plugin_extension("plugin.esl", GameId::Fallout4VR));
1✔
721
        assert!(has_plugin_extension("plugin.esl", GameId::Starfield));
1✔
722

723
        assert!(!has_plugin_extension("plugin.esl.ghost", GameId::OpenMW));
1✔
724
        assert!(!has_plugin_extension("plugin.esl.ghost", GameId::Morrowind));
1✔
725
        assert!(!has_plugin_extension("plugin.esl.ghost", GameId::Oblivion));
1✔
726
        assert!(!has_plugin_extension("plugin.esl.ghost", GameId::Skyrim));
1✔
727
        assert!(has_plugin_extension("plugin.esl.ghost", GameId::SkyrimSE));
1✔
728
        assert!(has_plugin_extension("plugin.esl.ghost", GameId::SkyrimVR));
1✔
729
        assert!(!has_plugin_extension("plugin.esl.ghost", GameId::Fallout3));
1✔
730
        assert!(!has_plugin_extension("plugin.esl.ghost", GameId::FalloutNV));
1✔
731
        assert!(has_plugin_extension("plugin.esl.ghost", GameId::Fallout4));
1✔
732
        assert!(has_plugin_extension("plugin.esl.ghost", GameId::Fallout4VR));
1✔
733
        assert!(has_plugin_extension("plugin.esl.ghost", GameId::Starfield));
1✔
734
    }
1✔
735

736
    #[test]
737
    fn trim_dot_ghost_should_trim_the_ghost_extension_if_the_game_allows_ghosting() {
1✔
738
        let ghosted = "plugin.esp.ghost";
1✔
739
        let unghosted = "plugin.esp";
1✔
740

1✔
741
        assert_eq!(ghosted, trim_dot_ghost(ghosted, GameId::OpenMW));
1✔
742
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::Morrowind));
1✔
743
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::Oblivion));
1✔
744
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::Skyrim));
1✔
745
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::SkyrimSE));
1✔
746
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::SkyrimVR));
1✔
747
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::Fallout3));
1✔
748
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::FalloutNV));
1✔
749
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::Fallout4));
1✔
750
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::Fallout4VR));
1✔
751
        assert_eq!(unghosted, trim_dot_ghost(ghosted, GameId::Starfield));
1✔
752
    }
1✔
753

754
    #[test]
755
    fn trim_dot_ghost_unchecked_should_trim_the_ghost_extension() {
1✔
756
        let ghosted = "plugin.esp.ghost";
1✔
757
        let unghosted = "plugin.esp";
1✔
758

1✔
759
        assert_eq!(unghosted, trim_dot_ghost_unchecked(ghosted));
1✔
760
        assert_eq!(unghosted, trim_dot_ghost_unchecked(unghosted));
1✔
761
    }
1✔
762
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc