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

Ortham / libloadorder / 12975146984

26 Jan 2025 01:32PM UTC coverage: 92.384% (+0.6%) from 91.744%
12975146984

push

github

Ortham
Add support for .omwscripts plugins

68 of 68 new or added lines in 1 file covered. (100.0%)

57 existing lines in 5 files now uncovered.

9001 of 9743 relevant lines covered (92.38%)

1486801.78 hits per line

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

99.19
/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,150✔
54
        Plugin::with_active(filename, game_settings, false)
20,150✔
55
    }
20,150✔
56

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

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

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

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

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

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

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

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

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

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

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

118
    pub fn name_matches(&self, string: &str) -> bool {
377,368,466✔
119
        eq(self.name(), trim_dot_ghost(self.game_id, string))
377,368,466✔
120
    }
377,368,466✔
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,091✔
127
        self.active
528,091✔
128
    }
528,091✔
129

130
    pub fn is_master_file(&self) -> bool {
86,904,755✔
131
        self.data.is_master_file()
86,904,755✔
132
    }
86,904,755✔
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,056,794✔
143
        self.data.is_blueprint_plugin() && self.is_master_file()
694,056,794✔
144
    }
694,056,794✔
145

146
    pub fn masters(&self) -> Result<Vec<String>, Error> {
43,422,699✔
147
        self.data
43,422,699✔
148
            .masters()
43,422,699✔
149
            .map_err(|e| file_error(self.data.path(), e))
43,422,699✔
150
    }
43,422,699✔
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,816✔
173
        if !self.is_active() {
11,816✔
174
            if self.game_id.allow_plugin_ghosting() {
11,815✔
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
            }
2✔
188

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

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

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

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

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

224
pub fn trim_dot_ghost(game_id: GameId, string: &str) -> &str {
377,390,021✔
225
    if game_id.allow_plugin_ghosting() {
377,390,021✔
226
        trim_dot_ghost_unchecked(string)
377,389,889✔
227
    } else {
228
        string
132✔
229
    }
230
}
377,390,021✔
231

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

235
    if iends_with_ascii(string, GHOST_FILE_EXTENSION) {
377,389,999✔
236
        &string[..(string.len() - GHOST_FILE_EXTENSION.len())]
26✔
237
    } else {
238
        string
377,389,973✔
239
    }
240
}
377,389,999✔
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_game};
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_game(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) => assert_eq!(game_path.join("Data Files").join(ghosted_name), p),
1✔
UNCOV
319
            e => panic!("Expected invalid path error, got {:?}", e),
×
320
        }
321
    }
1✔
322

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

1✔
328
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
329

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

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

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

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

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

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

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

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

1✔
368
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
369

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

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

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

1✔
383
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
384

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

1✔
388
        assert!(!plugin.is_active());
1✔
389
    }
1✔
390

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

1✔
396
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
397

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

1✔
401
        assert!(plugin.is_master_file());
1✔
402
    }
1✔
403

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

1✔
409
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
410

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

1✔
414
        assert!(!plugin.is_master_file());
1✔
415
    }
1✔
416

417
    #[test]
418
    fn is_master_file_should_be_false_for_a_omwscripts_plugin() {
1✔
419
        let tmp_dir = tempdir().unwrap();
1✔
420
        let game_dir = tmp_dir.path();
1✔
421

1✔
422
        let settings = openmw_game(game_dir);
1✔
423

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

1✔
428
        assert!(!plugin.is_master_file());
1✔
429
    }
1✔
430

431
    #[test]
432
    fn is_light_plugin_should_be_true_for_esl_files_only() {
1✔
433
        let tmp_dir = tempdir().unwrap();
1✔
434
        let game_dir = tmp_dir.path();
1✔
435

1✔
436
        let settings = game_settings(GameId::SkyrimSE, game_dir);
1✔
437

1✔
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_light_plugin());
1✔
447

448
        copy_to_test_dir("Blank.esm", "Blank.esl", &settings);
1✔
449
        let plugin = Plugin::new("Blank.esl", &settings).unwrap();
1✔
450

1✔
451
        assert!(plugin.is_light_plugin());
1✔
452

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

1✔
456
        assert!(plugin.is_light_plugin());
1✔
457
    }
1✔
458

459
    #[test]
460
    fn is_light_plugin_should_be_false_for_a_omwscripts_plugin() {
1✔
461
        let tmp_dir = tempdir().unwrap();
1✔
462
        let game_dir = tmp_dir.path();
1✔
463

1✔
464
        let settings = openmw_game(game_dir);
1✔
465

1✔
466
        let name = "plugin.omwscripts";
1✔
467
        std::fs::write(settings.plugins_directory().join(name), "").unwrap();
1✔
468
        let plugin = Plugin::new(name, &settings).unwrap();
1✔
469

1✔
470
        assert!(!plugin.is_light_plugin());
1✔
471
    }
1✔
472

473
    #[test]
474
    fn is_medium_plugin_should_be_false_for_a_omwscripts_plugin() {
1✔
475
        let tmp_dir = tempdir().unwrap();
1✔
476
        let game_dir = tmp_dir.path();
1✔
477

1✔
478
        let settings = openmw_game(game_dir);
1✔
479

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

1✔
484
        assert!(!plugin.is_light_plugin());
1✔
485
    }
1✔
486

487
    #[test]
488
    fn is_blueprint_master_should_be_false_for_a_omwscripts_plugin() {
1✔
489
        let tmp_dir = tempdir().unwrap();
1✔
490
        let game_dir = tmp_dir.path();
1✔
491

1✔
492
        let settings = openmw_game(game_dir);
1✔
493

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

1✔
498
        assert!(!plugin.is_blueprint_master());
1✔
499
    }
1✔
500

501
    #[test]
502
    fn masters_should_be_empty_for_a_omwscripts_plugin() {
1✔
503
        let tmp_dir = tempdir().unwrap();
1✔
504
        let game_dir = tmp_dir.path();
1✔
505

1✔
506
        let settings = openmw_game(game_dir);
1✔
507

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

1✔
512
        assert!(plugin.masters().unwrap().is_empty());
1✔
513
    }
1✔
514

515
    #[test]
516
    fn set_modification_time_should_update_the_file_modification_time() {
1✔
517
        let tmp_dir = tempdir().unwrap();
1✔
518
        let game_dir = tmp_dir.path();
1✔
519

1✔
520
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
521

1✔
522
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
523

1✔
524
        let path = game_dir.join("Data").join("Blank.esp");
1✔
525
        let file_size = path.metadata().unwrap().len();
1✔
526

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

1✔
529
        assert_ne!(UNIX_EPOCH, plugin.modification_time());
1✔
530
        plugin.set_modification_time(UNIX_EPOCH).unwrap();
1✔
531

1✔
532
        let metadata = path.metadata().unwrap();
1✔
533
        let new_mtime = metadata.modified().unwrap();
1✔
534
        let new_size = metadata.len();
1✔
535

1✔
536
        assert_eq!(UNIX_EPOCH, plugin.modification_time());
1✔
537
        assert_eq!(UNIX_EPOCH, new_mtime);
1✔
538
        assert_eq!(file_size, new_size);
1✔
539
    }
1✔
540

541
    #[test]
542
    fn set_modification_time_should_be_able_to_handle_pre_unix_timestamps() {
1✔
543
        let tmp_dir = tempdir().unwrap();
1✔
544
        let game_dir = tmp_dir.path();
1✔
545

1✔
546
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
547

1✔
548
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
549
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
550
        let target_mtime = UNIX_EPOCH - Duration::from_secs(1);
1✔
551

1✔
552
        assert_ne!(target_mtime, plugin.modification_time());
1✔
553
        plugin.set_modification_time(target_mtime).unwrap();
1✔
554
        let new_mtime = game_dir
1✔
555
            .join("Data")
1✔
556
            .join("Blank.esp")
1✔
557
            .metadata()
1✔
558
            .unwrap()
1✔
559
            .modified()
1✔
560
            .unwrap();
1✔
561

1✔
562
        assert_eq!(target_mtime, plugin.modification_time());
1✔
563
        assert_eq!(target_mtime, new_mtime);
1✔
564
    }
1✔
565

566
    #[test]
567
    fn activate_should_unghost_a_ghosted_plugin() {
1✔
568
        let tmp_dir = tempdir().unwrap();
1✔
569
        let game_dir = tmp_dir.path();
1✔
570

1✔
571
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
572

1✔
573
        copy_to_test_dir("Blank.esp", "Blank.esp.ghost", &settings);
1✔
574
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
575

1✔
576
        plugin.activate().unwrap();
1✔
577

1✔
578
        assert!(plugin.is_active());
1✔
579
        assert_eq!("Blank.esp", plugin.name());
1✔
580
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
581
    }
1✔
582

583
    #[test]
584
    fn activate_should_not_unghost_an_openmw_plugin() {
1✔
585
        // It's not possible to create an OpenMW Plugin from a path ending in
1✔
586
        // .ghost outside of this module, so this is just for internal
1✔
587
        // consistency.
1✔
588
        let tmp_dir = tempdir().unwrap();
1✔
589
        let game_dir = tmp_dir.path();
1✔
590

1✔
591
        let settings = openmw_game(game_dir);
1✔
592

1✔
593
        let plugin_name = "Blank.esp.ghost";
1✔
594
        copy_to_test_dir("Blank.esp", plugin_name, &settings);
1✔
595

1✔
596
        let data = esplugin::Plugin::new(
1✔
597
            GameId::OpenMW.to_esplugin_id(),
1✔
598
            &game_dir.join("Data Files").join(plugin_name),
1✔
599
        );
1✔
600

1✔
601
        let mut plugin = Plugin {
1✔
602
            active: false,
1✔
603
            modification_time: SystemTime::now(),
1✔
604
            data,
1✔
605
            name: plugin_name.to_string(),
1✔
606
            game_id: GameId::OpenMW,
1✔
607
        };
1✔
608

1✔
609
        plugin.activate().unwrap();
1✔
610
        assert!(plugin.is_active());
1✔
611
        assert_eq!(plugin_name, plugin.name());
1✔
612
    }
1✔
613

614
    #[test]
615
    fn deactivate_should_not_ghost_a_plugin() {
1✔
616
        let tmp_dir = tempdir().unwrap();
1✔
617
        let game_dir = tmp_dir.path();
1✔
618

1✔
619
        let settings = game_settings(GameId::Oblivion, game_dir);
1✔
620

1✔
621
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
1✔
622
        let mut plugin = Plugin::new("Blank.esp", &settings).unwrap();
1✔
623

1✔
624
        plugin.deactivate();
1✔
625

1✔
626
        assert!(!plugin.is_active());
1✔
627
        assert!(game_dir.join("Data").join("Blank.esp").exists());
1✔
628
    }
1✔
629

630
    #[test]
631
    fn trim_dot_ghost_should_trim_the_ghost_extension_if_the_game_allows_ghosting() {
1✔
632
        let ghosted = "plugin.esp.ghost";
1✔
633
        let unghosted = "plugin.esp";
1✔
634

1✔
635
        assert_eq!(ghosted, trim_dot_ghost(GameId::OpenMW, ghosted));
1✔
636
        assert_eq!(unghosted, trim_dot_ghost(GameId::Morrowind, ghosted));
1✔
637
        assert_eq!(unghosted, trim_dot_ghost(GameId::Oblivion, ghosted));
1✔
638
        assert_eq!(unghosted, trim_dot_ghost(GameId::Skyrim, ghosted));
1✔
639
        assert_eq!(unghosted, trim_dot_ghost(GameId::SkyrimSE, ghosted));
1✔
640
        assert_eq!(unghosted, trim_dot_ghost(GameId::SkyrimVR, ghosted));
1✔
641
        assert_eq!(unghosted, trim_dot_ghost(GameId::Fallout3, ghosted));
1✔
642
        assert_eq!(unghosted, trim_dot_ghost(GameId::FalloutNV, ghosted));
1✔
643
        assert_eq!(unghosted, trim_dot_ghost(GameId::Fallout4, ghosted));
1✔
644
        assert_eq!(unghosted, trim_dot_ghost(GameId::Fallout4VR, ghosted));
1✔
645
        assert_eq!(unghosted, trim_dot_ghost(GameId::Starfield, ghosted));
1✔
646
    }
1✔
647

648
    #[test]
649
    fn trim_dot_ghost_unchecked_should_trim_the_ghost_extension() {
1✔
650
        let ghosted = "plugin.esp.ghost";
1✔
651
        let unghosted = "plugin.esp";
1✔
652

1✔
653
        assert_eq!(unghosted, trim_dot_ghost_unchecked(ghosted));
1✔
654
        assert_eq!(unghosted, trim_dot_ghost_unchecked(ghosted));
1✔
655
    }
1✔
656
}
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