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

Ortham / libloadorder / 13081911206

31 Jan 2025 10:34PM UTC coverage: 92.365% (-0.008%) from 92.373%
13081911206

push

github

Ortham
Set versions and changelogs for 18.2.0

9473 of 10256 relevant lines covered (92.37%)

1568159.06 hits per line

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

99.15
/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,504✔
58
        filename: &str,
20,504✔
59
        game_settings: &GameSettings,
20,504✔
60
        active: bool,
20,504✔
61
    ) -> Result<Plugin, Error> {
20,504✔
62
        let filepath = game_settings.plugin_path(filename);
20,504✔
63

64
        let filepath = if game_settings.id().allow_plugin_ghosting() {
20,504✔
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
55✔
74
        };
75

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

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

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

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

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

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

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

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

118
    pub fn name_matches(&self, string: &str) -> bool {
420,545,319✔
119
        eq(self.name(), trim_dot_ghost(string, self.game_id))
420,545,319✔
120
    }
420,545,319✔
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,569✔
131
        self.game_id != GameId::OpenMW && self.data.is_master_file()
86,865,569✔
132
    }
86,865,569✔
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,145✔
143
        self.data.is_blueprint_plugin() && self.is_master_file()
780,425,145✔
144
    }
780,425,145✔
145

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

152
    pub fn has_master(&self, master: &str) -> bool {
94✔
153
        self.masters()
94✔
154
            .unwrap_or_default()
94✔
155
            .iter()
94✔
156
            .any(|m| eq(m.as_str(), master))
94✔
157
    }
94✔
158

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

34✔
169
        File::options()
34✔
170
            .write(true)
34✔
171
            .open(self.data.path())
34✔
172
            .and_then(|f| f.set_times(times))
34✔
173
            .map_err(|e| Error::IoError(self.data.path().to_path_buf(), e))?;
34✔
174

175
        self.modification_time = time;
34✔
176
        Ok(())
34✔
177
    }
34✔
178

179
    pub fn activate(&mut self) -> Result<(), Error> {
11,783✔
180
        if !self.is_active() {
11,783✔
181
            if self.game_id.allow_plugin_ghosting() {
11,783✔
182
                use crate::ghostable_path::GhostablePath;
183

184
                if self.data.path().has_ghost_extension() {
11,782✔
185
                    let new_path = self.data.path().unghost()?;
1✔
186

187
                    self.data = esplugin::Plugin::new(self.data.game_id(), &new_path);
1✔
188
                    self.data
1✔
189
                        .parse_file(ParseOptions::header_only())
1✔
190
                        .map_err(|e| file_error(self.data.path(), e))?;
1✔
191
                    let modification_time = self.modification_time();
1✔
192
                    self.set_modification_time(modification_time)?;
1✔
193
                }
11,781✔
194
            }
1✔
195

196
            self.active = true;
11,783✔
197
        }
×
198
        Ok(())
11,783✔
199
    }
11,783✔
200

201
    pub fn deactivate(&mut self) {
11,936✔
202
        self.active = false;
11,936✔
203
    }
11,936✔
204
}
205

206
pub fn has_plugin_extension(filename: &str, game: GameId) -> bool {
20,893✔
207
    let valid_extensions = if game == GameId::OpenMW {
20,893✔
208
        VALID_EXTENSIONS_OPENMW
100✔
209
    } else if game.supports_light_plugins() {
20,793✔
210
        VALID_EXTENSIONS_WITH_ESL
19,569✔
211
    } else {
212
        VALID_EXTENSIONS
1,224✔
213
    };
214

215
    valid_extensions
20,893✔
216
        .iter()
20,893✔
217
        .any(|e| iends_with_ascii(filename, e))
41,029✔
218
}
20,893✔
219

220
pub fn iends_with_ascii(string: &str, suffix: &str) -> bool {
420,627,213✔
221
    // as_bytes().into_iter() is faster than bytes().
420,627,213✔
222
    string.len() >= suffix.len()
420,627,213✔
223
        && string
420,626,773✔
224
            .as_bytes()
420,626,773✔
225
            .iter()
420,626,773✔
226
            .rev()
420,626,773✔
227
            .zip(suffix.as_bytes().iter().rev())
420,626,773✔
228
            .all(|(string_byte, suffix_byte)| string_byte.eq_ignore_ascii_case(suffix_byte))
420,690,199✔
229
}
420,627,213✔
230

231
pub fn trim_dot_ghost(string: &str, game_id: GameId) -> &str {
420,566,024✔
232
    if game_id.allow_plugin_ghosting() {
420,566,024✔
233
        trim_dot_ghost_unchecked(string)
420,565,707✔
234
    } else {
235
        string
317✔
236
    }
237
}
420,566,024✔
238

239
pub fn trim_dot_ghost_unchecked(string: &str) -> &str {
420,565,791✔
240
    use crate::ghostable_path::GHOST_FILE_EXTENSION;
241

242
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
420,565,791✔
243
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
23✔
244
    } else {
245
        string
420,565,768✔
246
    }
247
}
420,565,791✔
248

249
fn file_error(file_path: &Path, error: esplugin::Error) -> Error {
6✔
250
    match error {
6✔
251
        esplugin::Error::IoError(x) => Error::IoError(file_path.to_path_buf(), x),
6✔
252
        esplugin::Error::NoFilename(_) => Error::NoFilename(file_path.to_path_buf()),
×
253
        e => Error::PluginParsingError(file_path.to_path_buf(), Box::new(e)),
×
254
    }
255
}
6✔
256

257
#[cfg(test)]
258
mod tests {
259
    use super::*;
260

261
    use crate::tests::{copy_to_test_dir, create_file};
262
    use std::path::{Path, PathBuf};
263
    use std::time::{Duration, UNIX_EPOCH};
264
    use tempfile::tempdir;
265

266
    fn game_settings(game_id: GameId, game_path: &Path) -> GameSettings {
21✔
267
        GameSettings::with_local_and_my_games_paths(
21✔
268
            game_id,
21✔
269
            game_path,
21✔
270
            &PathBuf::default(),
21✔
271
            PathBuf::default(),
21✔
272
        )
21✔
273
        .unwrap()
21✔
274
    }
21✔
275

276
    #[test]
277
    fn with_active_should_unghost_active_ghosted_plugin_paths() {
1✔
278
        let tmp_dir = tempdir().unwrap();
1✔
279
        let game_dir = tmp_dir.path();
1✔
280

1✔
281
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
282

1✔
283
        let name = "Blank.esp";
1✔
284
        let ghosted_name = "Blank.esp.ghost";
1✔
285

1✔
286
        copy_to_test_dir(name, ghosted_name, &settings);
1✔
287
        let plugin = Plugin::with_active(ghosted_name, &settings, true).unwrap();
1✔
288

1✔
289
        assert_eq!(name, plugin.name());
1✔
290
        assert!(game_dir.join("Data").join(name).exists());
1✔
291
        assert!(!game_dir.join("Data").join(ghosted_name).exists());
1✔
292
    }
1✔
293

294
    #[test]
295
    fn with_active_should_resolve_inactive_ghosted_plugin_paths() {
1✔
296
        let tmp_dir = tempdir().unwrap();
1✔
297
        let game_dir = tmp_dir.path();
1✔
298

1✔
299
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
300

1✔
301
        let name = "Blank.esp";
1✔
302
        let ghosted_name = "Blank.esp.ghost";
1✔
303

1✔
304
        copy_to_test_dir(name, ghosted_name, &settings);
1✔
305
        let plugin = Plugin::with_active(ghosted_name, &settings, false).unwrap();
1✔
306

1✔
307
        assert_eq!(name, plugin.name());
1✔
308
        assert!(!game_dir.join("Data").join(name).exists());
1✔
309
        assert!(game_dir.join("Data").join(ghosted_name).exists());
1✔
310
    }
1✔
311

312
    #[test]
313
    fn with_active_should_not_resolve_ghosted_plugin_paths_for_openmw() {
1✔
314
        let tmp_dir = tempdir().unwrap();
1✔
315
        let game_dir = tmp_dir.path();
1✔
316

1✔
317
        let settings = game_settings(GameId::OpenMW, game_dir);
1✔
318

1✔
319
        let name = "Blank.esp";
1✔
320
        let ghosted_name = "Blank.esp.ghost";
1✔
321

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

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

1✔
336
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
337

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

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

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

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

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

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

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

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

1✔
376
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
377

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

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

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

1✔
391
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
392

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

1✔
396
        assert!(!plugin.is_active());
1✔
397
    }
1✔
398

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

1✔
404
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
405

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

1✔
409
        assert!(plugin.is_master_file());
1✔
410
    }
1✔
411

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

1✔
417
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
418

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

1✔
422
        assert!(!plugin.is_master_file());
1✔
423
    }
1✔
424

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

1✔
430
        let settings = game_settings(GameId::OpenMW, game_dir);
1✔
431

1✔
432
        let name = "plugin.omwscripts";
1✔
433
        create_file(&settings.plugins_directory().join(name));
1✔
434
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
435

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

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

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

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

1✔
446
        assert!(!plugin.is_master_file());
1✔
447
    }
1✔
448

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

1✔
454
        let settings = game_settings(GameId::SkyrimSE, game_dir);
1✔
455

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

1✔
459
        assert!(!plugin.is_master_file());
1✔
460

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

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

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

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

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

1✔
474
        assert!(plugin.is_light_plugin());
1✔
475
    }
1✔
476

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

1✔
482
        let settings = game_settings(GameId::OpenMW, game_dir);
1✔
483

1✔
484
        let name = "plugin.omwscripts";
1✔
485
        create_file(&settings.plugins_directory().join(name));
1✔
486
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
487

1✔
488
        assert!(!plugin.is_light_plugin());
1✔
489
    }
1✔
490

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

1✔
496
        let settings = game_settings(GameId::OpenMW, game_dir);
1✔
497

1✔
498
        let name = "plugin.omwscripts";
1✔
499
        create_file(&settings.plugins_directory().join(name));
1✔
500
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
501

1✔
502
        assert!(!plugin.is_light_plugin());
1✔
503
    }
1✔
504

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

1✔
510
        let settings = game_settings(GameId::OpenMW, game_dir);
1✔
511

1✔
512
        let name = "plugin.omwscripts";
1✔
513
        create_file(&settings.plugins_directory().join(name));
1✔
514
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
515

1✔
516
        assert!(!plugin.is_blueprint_master());
1✔
517
    }
1✔
518

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

1✔
524
        let settings = game_settings(GameId::OpenMW, game_dir);
1✔
525

1✔
526
        let name = "plugin.omwscripts";
1✔
527
        create_file(&settings.plugins_directory().join(name));
1✔
528
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
529

1✔
530
        assert!(plugin.masters().unwrap().is_empty());
1✔
531
    }
1✔
532

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

1✔
538
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
539

1✔
540
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
541

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

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

1✔
547
        assert_ne!(UNIX_EPOCH, plugin.modification_time());
1✔
548
        plugin.set_modification_time(UNIX_EPOCH).unwrap();
1✔
549

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

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

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

1✔
564
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
565

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

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

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

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

1✔
589
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
590

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

1✔
594
        plugin.activate().unwrap();
1✔
595

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

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

1✔
609
        let settings = game_settings(GameId::OpenMW, game_dir);
1✔
610

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

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

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

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

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

1✔
637
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
638

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

1✔
642
        plugin.deactivate();
1✔
643

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

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

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

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

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

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

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

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

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

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

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

1✔
761
        assert_eq!(unghosted, trim_dot_ghost_unchecked(ghosted));
1✔
762
        assert_eq!(unghosted, trim_dot_ghost_unchecked(unghosted));
1✔
763
    }
1✔
764
}
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