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

Ortham / libloadorder / 9669712859

25 Jun 2024 09:15PM UTC coverage: 91.599% (+4.7%) from 86.942%
9669712859

push

github

Ortham
Use Starfield plugins in Starfield tests

65 of 65 new or added lines in 5 files covered. (100.0%)

163 existing lines in 11 files now uncovered.

7218 of 7880 relevant lines covered (91.6%)

66616.03 hits per line

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

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

20
use std::convert::TryFrom;
21
use std::fmt::Display;
22
use std::fs::{create_dir_all, File, FileTimes, OpenOptions};
23
use std::io::{self, Seek, Write};
24
use std::path::Path;
25
use std::time::{Duration, SystemTime};
26

27
use crate::enums::GameId;
28
use crate::enums::LoadOrderMethod;
29
use crate::game_settings::GameSettings;
30
use crate::load_order::strict_encode;
31
use crate::plugin::Plugin;
32
use crate::tests::copy_to_test_dir;
33

34
use super::mutable::MutableLoadOrder;
35

36
pub fn write_load_order_file<T: AsRef<str> + Display>(
11✔
37
    game_settings: &GameSettings,
11✔
38
    filenames: &[T],
11✔
39
) {
11✔
40
    let mut file = File::create(&game_settings.load_order_file().unwrap()).unwrap();
11✔
41

42
    for filename in filenames {
54✔
43
        writeln!(file, "{}", filename).unwrap();
43✔
44
    }
43✔
45
}
11✔
46

47
pub fn write_active_plugins_file<T: AsRef<str>>(game_settings: &GameSettings, filenames: &[T]) {
38✔
48
    let mut file = File::create(&game_settings.active_plugins_file()).unwrap();
38✔
49

38✔
50
    if game_settings.id() == GameId::Morrowind {
38✔
51
        writeln!(file, "isrealmorrowindini=false").unwrap();
2✔
52
        writeln!(file, "[Game Files]").unwrap();
2✔
53
    }
36✔
54

55
    for filename in filenames {
15,919✔
56
        if game_settings.id() == GameId::Morrowind {
15,881✔
57
            write!(file, "GameFile0=").unwrap();
4✔
58
        } else if game_settings.load_order_method() == LoadOrderMethod::Asterisk {
15,877✔
59
            write!(file, "*").unwrap();
15,830✔
60
        }
15,830✔
61

62
        file.write_all(&strict_encode(filename.as_ref()).unwrap())
15,881✔
63
            .unwrap();
15,881✔
64
        writeln!(file, "").unwrap();
15,881✔
65
    }
66
}
38✔
67

68
pub fn set_timestamps<T: AsRef<str>>(plugins_directory: &Path, filenames: &[T]) {
4✔
69
    for (index, filename) in filenames.iter().enumerate() {
22✔
70
        set_file_timestamps(
22✔
71
            &plugins_directory.join(filename.as_ref()),
22✔
72
            u64::try_from(index).unwrap(),
22✔
73
        );
22✔
74
    }
22✔
75
}
4✔
76

77
pub fn set_file_timestamps(path: &Path, unix_seconds: u64) {
39✔
78
    let times = FileTimes::new()
39✔
79
        .set_accessed(SystemTime::UNIX_EPOCH)
39✔
80
        .set_modified(SystemTime::UNIX_EPOCH + Duration::from_secs(unix_seconds));
39✔
81
    File::options()
39✔
82
        .write(true)
39✔
83
        .open(path)
39✔
84
        .unwrap()
39✔
85
        .set_times(times)
39✔
86
        .unwrap();
39✔
87
}
39✔
88

89
pub fn game_settings_for_test(game_id: GameId, game_path: &Path) -> GameSettings {
216✔
90
    let local_path = game_path.join("local");
216✔
91
    create_dir_all(&local_path).unwrap();
216✔
92
    let my_games_path = game_path.join("my games");
216✔
93
    create_dir_all(&my_games_path).unwrap();
216✔
94

216✔
95
    GameSettings::with_local_and_my_games_paths(game_id, game_path, &local_path, my_games_path)
216✔
96
        .unwrap()
216✔
97
}
216✔
98

99
pub fn set_timestamp_order(plugin_names: &[&str], parent_path: &Path) {
201✔
100
    let mut timestamp = SystemTime::UNIX_EPOCH + Duration::from_secs(1321009871);
201✔
101
    for plugin_name in plugin_names {
1,407✔
102
        let path = parent_path.join(plugin_name);
1,206✔
103
        File::options()
1,206✔
104
            .write(true)
1,206✔
105
            .open(path)
1,206✔
106
            .unwrap()
1,206✔
107
            .set_modified(timestamp)
1,206✔
108
            .unwrap();
1,206✔
109
        timestamp += Duration::from_secs(60);
1,206✔
110
    }
1,206✔
111
}
201✔
112

113
pub fn mock_game_files(game_id: GameId, game_dir: &Path) -> (GameSettings, Vec<Plugin>) {
208✔
114
    let mut settings = game_settings_for_test(game_id, game_dir);
208✔
115

208✔
116
    if game_id == GameId::Starfield {
208✔
117
        copy_to_test_dir("Blank.full.esm", settings.master_file(), &settings);
7✔
118
        copy_to_test_dir("Blank.full.esm", "Blank.full.esm", &settings);
7✔
119
        copy_to_test_dir("Blank.medium.esm", "Blank.medium.esm", &settings);
7✔
120
        copy_to_test_dir("Blank.small.esm", "Blank.small.esm", &settings);
7✔
121
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
7✔
122
        copy_to_test_dir("Blank - Override.esp", "Blank - Override.esp", &settings);
7✔
123
    } else {
201✔
124
        copy_to_test_dir("Blank.esm", settings.master_file(), &settings);
201✔
125
        copy_to_test_dir("Blank.esm", "Blank.esm", &settings);
201✔
126
        copy_to_test_dir("Blank.esp", "Blank.esp", &settings);
201✔
127
        copy_to_test_dir("Blank - Different.esp", "Blank - Different.esp", &settings);
201✔
128
        copy_to_test_dir(
201✔
129
            "Blank - Master Dependent.esp",
201✔
130
            "Blank - Master Dependent.esp",
201✔
131
            &settings,
201✔
132
        );
201✔
133
        copy_to_test_dir("Blank.esp", "Blàñk.esp", &settings);
201✔
134

201✔
135
        let plugin_names = [
201✔
136
            settings.master_file(),
201✔
137
            "Blank.esm",
201✔
138
            "Blank.esp",
201✔
139
            "Blank - Different.esp",
201✔
140
            "Blank - Master Dependent.esp",
201✔
141
            "Blàñk.esp",
201✔
142
        ];
201✔
143
        set_timestamp_order(&plugin_names, &settings.plugins_directory());
201✔
144
    }
201✔
145

146
    // Refresh settings to account for newly-created plugin files.
147
    settings.refresh_implicitly_active_plugins().unwrap();
208✔
148

208✔
149
    let mut plugins = vec![
208✔
150
        Plugin::new(settings.master_file(), &settings).unwrap(),
208✔
151
        Plugin::with_active("Blank.esp", &settings, true).unwrap(),
208✔
152
    ];
208✔
153

208✔
154
    if game_id != GameId::Starfield {
208✔
155
        plugins.push(Plugin::new("Blank - Different.esp", &settings).unwrap());
201✔
156
    }
201✔
157

158
    (settings, plugins)
208✔
159
}
208✔
160

161
pub fn to_owned(strs: Vec<&str>) -> Vec<String> {
16✔
162
    strs.into_iter().map(String::from).collect()
16✔
163
}
16✔
164

165
/// Set the master flag to be present or not for the given plugin.
166
/// Only valid for plugins for games other than Morrowind.
167
pub fn set_master_flag(plugin: &Path, present: bool) -> io::Result<()> {
14✔
168
    let mut file = OpenOptions::new().write(true).open(plugin)?;
14✔
169
    file.seek(io::SeekFrom::Start(8))?;
14✔
170

171
    let value = if present { 1 } else { 0 };
14✔
172
    file.write(&[value])?;
14✔
173

174
    Ok(())
14✔
175
}
14✔
176

177
pub fn load_and_insert<T: MutableLoadOrder>(load_order: &mut T, plugin_name: &str) {
1,275✔
178
    let plugin = Plugin::new(plugin_name, load_order.game_settings()).unwrap();
1,275✔
179

1,275✔
180
    match load_order.insert_position(&plugin) {
1,275✔
UNCOV
181
        Some(position) => {
×
UNCOV
182
            load_order.plugins_mut().insert(position, plugin);
×
UNCOV
183
        }
×
184
        None => {
1,275✔
185
            load_order.plugins_mut().push(plugin);
1,275✔
186
        }
1,275✔
187
    }
188
}
1,275✔
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

© 2025 Coveralls, Inc