forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockfile.rs
More file actions
193 lines (173 loc) · 6.63 KB
/
Copy pathlockfile.rs
File metadata and controls
193 lines (173 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Reading and writing the `Nulang.lock` lockfile.
//!
//! The lockfile pins the exact source each resolved dependency was fetched
//! from, so builds are reproducible:
//!
//! ```toml
//! version = 1
//!
//! [[package]]
//! name = "util"
//! version = "0.1.0"
//! source = "path+/home/david/projects/util"
//!
//! [[package]]
//! name = "json"
//! version = "0.2.0"
//! source = "git+https://github.com/example/json.nu.git#v0.2.0"
//! commit = "a1b2c3d4e5f6..."
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::types::{NuError, NuResult, Span};
/// Lockfile name, written next to the root package's manifest.
pub const LOCKFILE_FILE: &str = "Nulang.lock";
/// Current on-disk lockfile format version.
pub const LOCKFILE_VERSION: u32 = 1;
/// A parsed `Nulang.lock`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Lockfile {
pub version: u32,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub package: Vec<LockedPackage>,
}
/// One pinned dependency.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LockedPackage {
pub name: String,
pub version: String,
/// `path+<dir>` for local dependencies, `git+<url>#<rev>` for git ones.
pub source: String,
/// BLAKE3 hash of the resolved source (hex). A module pinned by content
/// hash in 2026 is bit-identically resolvable in 2226 if any conforming
/// registry mirrors that hash. Empty string if the hash was not computed
/// (e.g. the source was unavailable at lock time).
#[serde(default, skip_serializing_if = "String::is_empty")]
pub content_hash: String,
/// Resolved git commit (full SHA) for `git+` sources, recorded at fetch
/// time so a later resolution can detect a moved branch/tag and re-fetch
/// the dependency. Empty for non-git sources.
#[serde(default, skip_serializing_if = "String::is_empty")]
pub commit: String,
}
impl Lockfile {
/// An empty lockfile at the current format version.
pub fn new() -> Self {
Lockfile {
version: LOCKFILE_VERSION,
package: Vec::new(),
}
}
/// Serialize to TOML text.
pub fn to_toml(&self) -> NuResult<String> {
toml::to_string_pretty(self).map_err(|e| NuError::PackageError {
msg: format!("cannot serialize lockfile: {}", e),
span: Span::default(),
})
}
/// Parse lockfile TOML text.
pub fn parse(source: &str) -> NuResult<Lockfile> {
let lockfile: Lockfile = toml::from_str(source).map_err(|e| NuError::PackageError {
msg: format!("invalid {}: {}", LOCKFILE_FILE, e),
span: Span::default(),
})?;
if lockfile.version != LOCKFILE_VERSION {
return Err(NuError::PackageError {
msg: format!(
"unsupported {} version {} (expected {})",
LOCKFILE_FILE, lockfile.version, LOCKFILE_VERSION
),
span: Span::default(),
});
}
Ok(lockfile)
}
/// Write the lockfile into `dir`.
pub fn save(&self, dir: &Path) -> NuResult<()> {
let path = dir.join(LOCKFILE_FILE);
std::fs::write(&path, self.to_toml()?).map_err(|e| NuError::PackageError {
msg: format!("cannot write {}: {}", path.display(), e),
span: Span::default(),
})
}
/// Read the lockfile from `dir`.
pub fn load(dir: &Path) -> NuResult<Lockfile> {
let path = dir.join(LOCKFILE_FILE);
let source = std::fs::read_to_string(&path).map_err(|e| NuError::PackageError {
msg: format!("cannot read {}: {}", path.display(), e),
span: Span::default(),
})?;
Self::parse(&source)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_lockfile() -> Lockfile {
Lockfile {
version: LOCKFILE_VERSION,
package: vec![
LockedPackage {
name: "util".to_string(),
version: "0.1.0".to_string(),
source: "path+/home/david/projects/util".to_string(),
content_hash: "aabbcc".to_string(),
commit: String::new(),
},
LockedPackage {
name: "json".to_string(),
version: "0.2.0".to_string(),
source: "git+https://github.com/example/json.nu.git#v0.2.0".to_string(),
content_hash: String::new(),
commit: "a1b2c3d4e5f67890abcdef1234567890abcdef12".to_string(),
},
],
}
}
#[test]
fn test_lockfile_toml_round_trip() {
let lockfile = sample_lockfile();
let toml_text = lockfile.to_toml().expect("lockfile should serialize");
let parsed = Lockfile::parse(&toml_text).expect("lockfile should re-parse");
assert_eq!(lockfile, parsed);
}
#[test]
fn test_lockfile_file_round_trip() {
let dir = std::env::temp_dir().join(format!("nulang_lockfile_test_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).expect("scratch dir should be created");
let lockfile = sample_lockfile();
lockfile.save(&dir).expect("lockfile should save");
assert!(dir.join(LOCKFILE_FILE).exists());
let loaded = Lockfile::load(&dir).expect("lockfile should load");
assert_eq!(lockfile, loaded);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn test_lockfile_rejects_unknown_version() {
let source = "version = 99\n";
let err = Lockfile::parse(source).expect_err("future versions must be rejected");
match err {
NuError::PackageError { msg, .. } => assert!(msg.contains("version 99")),
other => panic!("expected PackageError, got {:?}", other),
}
}
#[test]
fn test_lockfile_content_hash_round_trips() {
// A non-empty content_hash must survive serialization + re-parse.
let mut lockfile = Lockfile::new();
lockfile.package.push(LockedPackage {
name: "pinned".to_string(),
version: "1.0.0".to_string(),
source: "path+/tmp/pinned".to_string(),
content_hash: "deadbeef".to_string(),
commit: String::new(),
});
let toml_text = lockfile.to_toml().expect("serialize");
assert!(
toml_text.contains("content_hash"),
"content_hash must be in TOML: {toml_text}"
);
let parsed = Lockfile::parse(&toml_text).expect("parse");
assert_eq!(parsed.package[0].content_hash, "deadbeef");
}
}