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

Ortham / libloadorder / 13016131752

28 Jan 2025 05:30PM UTC coverage: 92.625% (-0.009%) from 92.634%
13016131752

push

github

Ortham
Deprecate GameSettings::master_file()

The master file isn't really useful on its own.

9344 of 10088 relevant lines covered (92.62%)

1594148.97 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> {
19,914✔
54
        Plugin::with_active(filename, game_settings, false)
19,914✔
55
    }
19,914✔
56

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

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

67
            if active {
20,449✔
68
                filepath.unghost()?
389✔
69
            } else {
70
                filepath.resolve_path()?
20,060✔
71
            }
72
        } else {
73
            filepath
50✔
74
        };
75

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

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

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

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

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

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

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

114
    pub fn name(&self) -> &str {
420,677,993✔
115
        &self.name
420,677,993✔
116
    }
420,677,993✔
117

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

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

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

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

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

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

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

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

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

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

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

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

177
                if self.data.path().has_ghost_extension() {
11,782✔
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,781✔
187
            }
1✔
188

189
            self.active = true;
11,783✔
190
        }
×
191
        Ok(())
11,783✔
192
    }
11,783✔
193

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

199
pub fn has_plugin_extension(filename: &str, game: GameId) -> bool {
20,884✔
200
    let valid_extensions = if game == GameId::OpenMW {
20,884✔
201
        VALID_EXTENSIONS_OPENMW
91✔
202
    } else if game.supports_light_plugins() {
20,793✔
203
        VALID_EXTENSIONS_WITH_ESL
19,569✔
204
    } else {
205
        VALID_EXTENSIONS
1,224✔
206
    };
207

208
    valid_extensions
20,884✔
209
        .iter()
20,884✔
210
        .any(|e| iends_with_ascii(filename, e))
41,016✔
211
}
20,884✔
212

213
fn iends_with_ascii(string: &str, suffix: &str) -> bool {
420,582,335✔
214
    // as_bytes().into_iter() is faster than bytes().
420,582,335✔
215
    string.len() >= suffix.len()
420,582,335✔
216
        && string
420,581,897✔
217
            .as_bytes()
420,581,897✔
218
            .iter()
420,581,897✔
219
            .rev()
420,581,897✔
220
            .zip(suffix.as_bytes().iter().rev())
420,581,897✔
221
            .all(|(string_byte, suffix_byte)| string_byte.eq_ignore_ascii_case(suffix_byte))
420,645,267✔
222
}
420,582,335✔
223

224
pub fn trim_dot_ghost(string: &str, game_id: GameId) -> &str {
420,521,082✔
225
    if game_id.allow_plugin_ghosting() {
420,521,082✔
226
        trim_dot_ghost_unchecked(string)
420,520,880✔
227
    } else {
228
        string
202✔
229
    }
230
}
420,521,082✔
231

232
pub fn trim_dot_ghost_unchecked(string: &str) -> &str {
420,520,964✔
233
    use crate::ghostable_path::GHOST_FILE_EXTENSION;
234

235
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
420,520,964✔
236
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
23✔
237
    } else {
238
        string
420,520,941✔
239
    }
240
}
420,520,964✔
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
        GameSettings::with_local_and_my_games_paths(
14✔
261
            game_id,
14✔
262
            game_path,
14✔
263
            &PathBuf::default(),
14✔
264
            PathBuf::default(),
14✔
265
        )
14✔
266
        .unwrap()
14✔
267
    }
14✔
268

269
    #[test]
270
    fn with_active_should_unghost_active_ghosted_plugin_paths() {
1✔
271
        let tmp_dir = tempdir().unwrap();
1✔
272
        let game_dir = tmp_dir.path();
1✔
273

1✔
274
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
275

1✔
276
        let name = "Blank.esp";
1✔
277
        let ghosted_name = "Blank.esp.ghost";
1✔
278

1✔
279
        copy_to_test_dir(name, ghosted_name, &settings);
1✔
280
        let plugin = Plugin::with_active(ghosted_name, &settings, true).unwrap();
1✔
281

1✔
282
        assert_eq!(name, plugin.name());
1✔
283
        assert!(game_dir.join("Data").join(name).exists());
1✔
284
        assert!(!game_dir.join("Data").join(ghosted_name).exists());
1✔
285
    }
1✔
286

287
    #[test]
288
    fn with_active_should_resolve_inactive_ghosted_plugin_paths() {
1✔
289
        let tmp_dir = tempdir().unwrap();
1✔
290
        let game_dir = tmp_dir.path();
1✔
291

1✔
292
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
293

1✔
294
        let name = "Blank.esp";
1✔
295
        let ghosted_name = "Blank.esp.ghost";
1✔
296

1✔
297
        copy_to_test_dir(name, ghosted_name, &settings);
1✔
298
        let plugin = Plugin::with_active(ghosted_name, &settings, false).unwrap();
1✔
299

1✔
300
        assert_eq!(name, plugin.name());
1✔
301
        assert!(!game_dir.join("Data").join(name).exists());
1✔
302
        assert!(game_dir.join("Data").join(ghosted_name).exists());
1✔
303
    }
1✔
304

305
    #[test]
306
    fn with_active_should_not_resolve_ghosted_plugin_paths_for_openmw() {
1✔
307
        let tmp_dir = tempdir().unwrap();
1✔
308
        let tmp_path = tmp_dir.path();
1✔
309
        let game_path = tmp_path.join("game");
1✔
310

1✔
311
        let settings = openmw_settings(tmp_path);
1✔
312

1✔
313
        let name = "Blank.esp";
1✔
314
        let ghosted_name = "Blank.esp.ghost";
1✔
315

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

325
    #[test]
326
    fn name_should_return_the_plugin_filename_without_any_ghost_extension() {
1✔
327
        let tmp_dir = tempdir().unwrap();
1✔
328
        let game_dir = tmp_dir.path();
1✔
329

1✔
330
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
331

1✔
332
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
333
        let plugin = Plugin::new("Blank.esp.ghost", &settings).unwrap();
1✔
334
        assert_eq!("Blank.esp", plugin.name());
1✔
335

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

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

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

1✔
351
        let plugin = Plugin::new("BlanK.esp.GHoSt", &settings).unwrap();
1✔
352
        assert!(plugin.name_matches("Blank.esp"));
1✔
353
    }
1✔
354

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

1✔
361
        let plugin = Plugin::new("BlanK.esp", &settings).unwrap();
1✔
362
        assert!(plugin.name_matches("Blank.esp.GHoSt"));
1✔
363
    }
1✔
364

365
    #[test]
366
    fn modification_time_should_return_the_plugin_modification_time_at_creation() {
1✔
367
        let tmp_dir = tempdir().unwrap();
1✔
368
        let game_dir = tmp_dir.path();
1✔
369

1✔
370
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
371

1✔
372
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
373
        let plugin_path = game_dir.join("Data").join("Blank.esp");
1✔
374
        let mtime = plugin_path.metadata().unwrap().modified().unwrap();
1✔
375

1✔
376
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
377
        assert_eq!(mtime, plugin.modification_time());
1✔
378
    }
1✔
379

380
    #[test]
381
    fn is_active_should_be_false() {
1✔
382
        let tmp_dir = tempdir().unwrap();
1✔
383
        let game_dir = tmp_dir.path();
1✔
384

1✔
385
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
386

1✔
387
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
388
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
389

1✔
390
        assert!(!plugin.is_active());
1✔
391
    }
1✔
392

393
    #[test]
394
    fn is_master_file_should_be_true_if_the_plugin_is_a_master_file() {
1✔
395
        let tmp_dir = tempdir().unwrap();
1✔
396
        let game_dir = tmp_dir.path();
1✔
397

1✔
398
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
399

1✔
400
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
1✔
401
        let plugin = Plugin::new("Blank.esm", &settings).unwrap();
1✔
402

1✔
403
        assert!(plugin.is_master_file());
1✔
404
    }
1✔
405

406
    #[test]
407
    fn is_master_file_should_be_false_if_the_plugin_is_not_a_master_file() {
1✔
408
        let tmp_dir = tempdir().unwrap();
1✔
409
        let game_dir = tmp_dir.path();
1✔
410

1✔
411
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
412

1✔
413
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
414
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
415

1✔
416
        assert!(!plugin.is_master_file());
1✔
417
    }
1✔
418

419
    #[test]
420
    fn is_master_file_should_be_false_for_all_openmw_plugins() {
1✔
421
        let tmp_dir = tempdir().unwrap();
1✔
422
        let game_dir = tmp_dir.path();
1✔
423

1✔
424
        let settings = openmw_settings(game_dir);
1✔
425

1✔
426
        let name = "plugin.omwscripts";
1✔
427
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
428
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
429

1✔
430
        assert!(!plugin.is_master_file());
1✔
431

432
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
433
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
434

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

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

1✔
440
        assert!(!plugin.is_master_file());
1✔
441
    }
1✔
442

443
    #[test]
444
    fn is_light_plugin_should_be_true_for_esl_files_only() {
1✔
445
        let tmp_dir = tempdir().unwrap();
1✔
446
        let game_dir = tmp_dir.path();
1✔
447

1✔
448
        let settings = game_settings(GameId::SkyrimSE, game_dir);
1✔
449

1✔
450
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
451
        let plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
452

1✔
453
        assert!(!plugin.is_master_file());
1✔
454

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

1✔
458
        assert!(!plugin.is_light_plugin());
1✔
459

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

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

465
        copy_to_test_dir("Blank - Different.esp", "Blank - Different.esl", &settings);
1✔
466
        let plugin = Plugin::new("Blank - Different.esl", &settings).unwrap();
1✔
467

1✔
468
        assert!(plugin.is_light_plugin());
1✔
469
    }
1✔
470

471
    #[test]
472
    fn is_light_plugin_should_be_false_for_an_omwscripts_plugin() {
1✔
473
        let tmp_dir = tempdir().unwrap();
1✔
474
        let game_dir = tmp_dir.path();
1✔
475

1✔
476
        let settings = openmw_settings(game_dir);
1✔
477

1✔
478
        let name = "plugin.omwscripts";
1✔
479
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
480
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
481

1✔
482
        assert!(!plugin.is_light_plugin());
1✔
483
    }
1✔
484

485
    #[test]
486
    fn is_medium_plugin_should_be_false_for_an_omwscripts_plugin() {
1✔
487
        let tmp_dir = tempdir().unwrap();
1✔
488
        let game_dir = tmp_dir.path();
1✔
489

1✔
490
        let settings = openmw_settings(game_dir);
1✔
491

1✔
492
        let name = "plugin.omwscripts";
1✔
493
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
494
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
495

1✔
496
        assert!(!plugin.is_light_plugin());
1✔
497
    }
1✔
498

499
    #[test]
500
    fn is_blueprint_master_should_be_false_for_an_omwscripts_plugin() {
1✔
501
        let tmp_dir = tempdir().unwrap();
1✔
502
        let game_dir = tmp_dir.path();
1✔
503

1✔
504
        let settings = openmw_settings(game_dir);
1✔
505

1✔
506
        let name = "plugin.omwscripts";
1✔
507
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
508
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
509

1✔
510
        assert!(!plugin.is_blueprint_master());
1✔
511
    }
1✔
512

513
    #[test]
514
    fn masters_should_be_empty_for_an_omwscripts_plugin() {
1✔
515
        let tmp_dir = tempdir().unwrap();
1✔
516
        let game_dir = tmp_dir.path();
1✔
517

1✔
518
        let settings = openmw_settings(game_dir);
1✔
519

1✔
520
        let name = "plugin.omwscripts";
1✔
521
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
522
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
523

1✔
524
        assert!(plugin.masters().unwrap().is_empty());
1✔
525
    }
1✔
526

527
    #[test]
528
    fn set_modification_time_should_update_the_file_modification_time() {
1✔
529
        let tmp_dir = tempdir().unwrap();
1✔
530
        let game_dir = tmp_dir.path();
1✔
531

1✔
532
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
533

1✔
534
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
535

1✔
536
        let path = game_dir.join("Data").join("Blank.esp");
1✔
537
        let file_size = path.metadata().unwrap().len();
1✔
538

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

1✔
541
        assert_ne!(UNIX_EPOCH, plugin.modification_time());
1✔
542
        plugin.set_modification_time(UNIX_EPOCH).unwrap();
1✔
543

1✔
544
        let metadata = path.metadata().unwrap();
1✔
545
        let new_mtime = metadata.modified().unwrap();
1✔
546
        let new_size = metadata.len();
1✔
547

1✔
548
        assert_eq!(UNIX_EPOCH, plugin.modification_time());
1✔
549
        assert_eq!(UNIX_EPOCH, new_mtime);
1✔
550
        assert_eq!(file_size, new_size);
1✔
551
    }
1✔
552

553
    #[test]
554
    fn set_modification_time_should_be_able_to_handle_pre_unix_timestamps() {
1✔
555
        let tmp_dir = tempdir().unwrap();
1✔
556
        let game_dir = tmp_dir.path();
1✔
557

1✔
558
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
559

1✔
560
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
561
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
562
        let target_mtime = UNIX_EPOCH - Duration::from_secs(1);
1✔
563

1✔
564
        assert_ne!(target_mtime, plugin.modification_time());
1✔
565
        plugin.set_modification_time(target_mtime).unwrap();
1✔
566
        let new_mtime = game_dir
1✔
567
            .join("Data")
1✔
568
            .join("Blank.esp")
1✔
569
            .metadata()
1✔
570
            .unwrap()
1✔
571
            .modified()
1✔
572
            .unwrap();
1✔
573

1✔
574
        assert_eq!(target_mtime, plugin.modification_time());
1✔
575
        assert_eq!(target_mtime, new_mtime);
1✔
576
    }
1✔
577

578
    #[test]
579
    fn activate_should_unghost_a_ghosted_plugin() {
1✔
580
        let tmp_dir = tempdir().unwrap();
1✔
581
        let game_dir = tmp_dir.path();
1✔
582

1✔
583
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
584

1✔
585
        copy_to_test_dir("Blank.esp", "Blank.esp.ghost", &settings);
1✔
586
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
587

1✔
588
        plugin.activate().unwrap();
1✔
589

1✔
590
        assert!(plugin.is_active());
1✔
591
        assert_eq!("Blank.esp", plugin.name());
1✔
592
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
593
    }
1✔
594

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

1✔
603
        let settings = openmw_settings(game_dir);
1✔
604

1✔
605
        let plugin_name = "Blank.esp.ghost";
1✔
606
        copy_to_test_dir("Blank.esp", plugin_name, &settings);
1✔
607

1✔
608
        let data = esplugin::Plugin::new(
1✔
609
            GameId::OpenMW.to_esplugin_id(),
1✔
610
            &game_dir.join("Data Files").join(plugin_name),
1✔
611
        );
1✔
612

1✔
613
        let mut plugin = Plugin {
1✔
614
            active: false,
1✔
615
            modification_time: SystemTime::now(),
1✔
616
            data,
1✔
617
            name: plugin_name.to_string(),
1✔
618
            game_id: GameId::OpenMW,
1✔
619
        };
1✔
620

1✔
621
        plugin.activate().unwrap();
1✔
622
        assert!(plugin.is_active());
1✔
623
        assert_eq!(plugin_name, plugin.name());
1✔
624
    }
1✔
625

626
    #[test]
627
    fn deactivate_should_not_ghost_a_plugin() {
1✔
628
        let tmp_dir = tempdir().unwrap();
1✔
629
        let game_dir = tmp_dir.path();
1✔
630

1✔
631
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
632

1✔
633
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
634
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
635

1✔
636
        plugin.deactivate();
1✔
637

1✔
638
        assert!(!plugin.is_active());
1✔
639
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
640
    }
1✔
641

642
    #[test]
643
    fn has_plugin_extension_should_recognise_openmw_extensions_for_openmw() {
1✔
644
        assert!(has_plugin_extension("plugin.omwgame", GameId::OpenMW));
1✔
645
        assert!(has_plugin_extension("plugin.omwaddon", GameId::OpenMW));
1✔
646
        assert!(has_plugin_extension("plugin.omwscripts", GameId::OpenMW));
1✔
647
    }
1✔
648

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

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

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

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

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

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

732
    #[test]
733
    fn trim_dot_ghost_should_trim_the_ghost_extension_if_the_game_allows_ghosting() {
1✔
734
        let ghosted = "plugin.esp.ghost";
1✔
735
        let unghosted = "plugin.esp";
1✔
736

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

750
    #[test]
751
    fn trim_dot_ghost_unchecked_should_trim_the_ghost_extension() {
1✔
752
        let ghosted = "plugin.esp.ghost";
1✔
753
        let unghosted = "plugin.esp";
1✔
754

1✔
755
        assert_eq!(unghosted, trim_dot_ghost_unchecked(ghosted));
1✔
756
        assert_eq!(unghosted, trim_dot_ghost_unchecked(unghosted));
1✔
757
    }
1✔
758
}
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