pop-common 0.14.0

Library that provides a collection of essential utilities and shared functionality for pop.
Documentation
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// SPDX-License-Identifier: GPL-3.0

use crate::{
	Error,
	manifest::{from_path, get_workspace_project_names},
};
use std::{
	collections::HashMap,
	fs,
	io::{Read, Write},
	path::{Path, PathBuf},
	process::Command,
};

/// Replaces occurrences of specified strings in a file with new values.
///
/// # Arguments
///
/// * `file_path` - A `PathBuf` specifying the path to the file to be modified.
/// * `replacements` - A `HashMap` where each key-value pair represents a target string and its
///   corresponding replacement string.
pub fn replace_in_file(file_path: PathBuf, replacements: HashMap<&str, &str>) -> Result<(), Error> {
	// Read the file content
	let mut file_content = String::new();
	fs::File::open(&file_path)?.read_to_string(&mut file_content)?;
	// Perform the replacements
	let mut modified_content = file_content;
	for (target, replacement) in &replacements {
		modified_content = modified_content.replace(target, replacement);
	}
	// Write the modified content back to the file
	let mut file = fs::File::create(&file_path)?;
	file.write_all(modified_content.as_bytes())?;
	Ok(())
}

/// Gets the last component (name of a project) of a path or returns a default value if the path has
/// no valid last component.
///
/// # Arguments
/// * `path` - Location path of the project.
/// * `default` - The default string to return if the path has no valid last component.
pub fn get_project_name_from_path<'a>(path: &'a Path, default: &'a str) -> String {
	path.file_name()
		.and_then(|name| name.to_str())
		.unwrap_or(default)
		.replace("-", "_")
}

/// Returns the relative path from `base` to `full` if `full` is inside `base`.
/// If `full` is outside `base`, returns the absolute path instead.
///
/// # Arguments
/// * `base` - The base directory to compare against.
/// * `full` - The full path to be shortened.
pub fn get_relative_or_absolute_path(base: &Path, full: &Path) -> PathBuf {
	match full.strip_prefix(base) {
		Ok(relative) => relative.to_path_buf(),
		// If prefix is different, return the full path
		Err(_) => full.to_path_buf(),
	}
}

/// Finds a built ink! contract artifact by searching project and workspace target/ink directories.
pub fn find_contract_artifact_path(project_root: &Path, package_name: &str) -> Option<PathBuf> {
	let mut ink_dirs = vec![project_root.join("target").join("ink")];
	if let Some(workspace_root) = find_workspace_root(project_root) {
		ink_dirs.push(workspace_root.join("target").join("ink"));
	}

	let artifact = format!("{package_name}.contract");
	ink_dirs
		.into_iter()
		.map(|ink_dir| ink_dir.join(&artifact))
		.find(|path| path.exists())
}

/// Walks up from `start` to find the nearest Cargo workspace root that includes the given path.
pub fn find_workspace_root(start: &Path) -> Option<PathBuf> {
	let start = start.canonicalize().ok().unwrap_or_else(|| start.to_path_buf());
	let mut current = Some(start.as_path());
	while let Some(dir) = current {
		let manifest_path = dir.join("Cargo.toml");
		if manifest_path.is_file() &&
			let Ok(manifest) = from_path(dir) &&
			manifest.workspace.is_some() &&
			workspace_includes_project(dir, &start)
		{
			return Some(dir.to_path_buf());
		}
		current = dir.parent();
	}
	None
}

fn workspace_includes_project(workspace_root: &Path, project_root: &Path) -> bool {
	get_workspace_project_names(workspace_root)
		.map(|members| {
			members.into_iter().map(|(_, member_path)| member_path).any(|member_path| {
				project_root == member_path || project_root.starts_with(&member_path)
			})
		})
		.unwrap_or(false)
}

/// Temporarily changes the current working directory while executing a closure.
pub fn with_current_dir<F, R>(dir: &Path, f: F) -> anyhow::Result<R>
where
	F: FnOnce() -> anyhow::Result<R>,
{
	let original_dir = std::env::current_dir()?;
	std::env::set_current_dir(dir)?;
	let result = f();
	std::env::set_current_dir(original_dir)?;
	result
}

/// Temporarily changes the current working directory while executing an asynchronous closure.
pub async fn with_current_dir_async<F, R>(dir: &Path, f: F) -> anyhow::Result<R>
where
	F: AsyncFnOnce() -> anyhow::Result<R>,
{
	let original_dir = std::env::current_dir()?;
	std::env::set_current_dir(dir)?;
	let result = f().await;
	std::env::set_current_dir(original_dir)?;
	result
}

/// Check if the current process is running as root (UID 0).
///
/// Returns `true` if running as root, `false` otherwise.
pub fn is_root() -> bool {
	Command::new("id")
		.arg("-u")
		.output()
		.ok()
		.and_then(|output| String::from_utf8(output.stdout).ok())
		.and_then(|s| s.trim().parse::<u32>().ok())
		.map(|uid| uid == 0)
		.unwrap_or(false)
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::command_mock::CommandMock;
	use anyhow::Result;
	use std::{
		fs,
		sync::{Mutex, OnceLock},
	};

	// Changing the current working directory is a global, process-wide side effect.
	// Serialize such tests to avoid flakiness when tests run in parallel.
	static CWD_TEST_MUTEX: OnceLock<Mutex<()>> = OnceLock::new();

	fn cwd_lock() -> std::sync::MutexGuard<'static, ()> {
		CWD_TEST_MUTEX.get_or_init(|| Mutex::new(())).lock().unwrap()
	}

	#[test]
	fn test_replace_in_file() -> Result<(), Error> {
		let temp_dir = tempfile::tempdir()?;
		let file_path = temp_dir.path().join("file.toml");
		let mut file = fs::File::create(temp_dir.path().join("file.toml"))?;
		writeln!(file, "name = test, version = 5.0.0")?;
		let mut replacements_in_cargo = HashMap::new();
		replacements_in_cargo.insert("test", "changed_name");
		replacements_in_cargo.insert("5.0.0", "5.0.1");
		replace_in_file(file_path.clone(), replacements_in_cargo)?;
		let content = fs::read_to_string(file_path).expect("Could not read file");
		assert_eq!(content.trim(), "name = changed_name, version = 5.0.1");
		Ok(())
	}

	#[test]
	fn get_project_name_from_path_works() -> Result<(), Error> {
		let path = Path::new("./path/to/project/my-parachain");
		assert_eq!(get_project_name_from_path(path, "default_name"), "my_parachain");
		Ok(())
	}

	#[test]
	fn get_project_name_from_path_default_value() -> Result<(), Error> {
		let path = Path::new("./");
		assert_eq!(get_project_name_from_path(path, "my-contract"), "my_contract");
		Ok(())
	}

	#[test]
	fn get_relative_or_absolute_path_works() {
		[
			("/path/to/project", "/path/to/project", ""),
			("/path/to/project", "/path/to/src", "/path/to/src"),
			("/path/to/project", "/path/to/project/main.rs", "main.rs"),
			("/path/to/project", "/path/to/project/../main.rs", "../main.rs"),
			("/path/to/project", "/path/to/project/src/main.rs", "src/main.rs"),
		]
		.into_iter()
		.for_each(|(base, full, expected)| {
			assert_eq!(
				get_relative_or_absolute_path(Path::new(base), Path::new(full)),
				Path::new(expected)
			);
		});
	}

	#[test]
	fn with_current_dir_changes_and_restores_cwd() -> anyhow::Result<()> {
		let _guard = cwd_lock();
		let original = std::env::current_dir()?;
		let temp_dir = tempfile::tempdir()?;
		let tmp_path = temp_dir.path().to_path_buf();

		let res: &str = with_current_dir(&tmp_path, || {
			// Inside the closure, the cwd should be the temp dir (canonicalized for macOS /private
			// symlink).
			let cwd = std::env::current_dir().unwrap();
			assert_eq!(cwd.canonicalize().unwrap(), tmp_path.canonicalize().unwrap());
			// Create a file relative to the new cwd to verify it's applied.
			fs::write("hello.txt", b"world").unwrap();
			Ok("done")
		})?;
		assert_eq!(res, "done");

		// After the closure, cwd should be restored.
		assert_eq!(std::env::current_dir()?, original);
		// The file should exist inside the temp dir.
		assert!(tmp_path.join("hello.txt").exists());
		Ok(())
	}

	#[tokio::test]
	async fn with_current_dir_async_changes_and_restores_cwd() -> anyhow::Result<()> {
		// Acquire and drop the mutex guard before async operations
		{
			let _guard = cwd_lock();
		}

		let original = std::env::current_dir()?;
		let temp_dir = tempfile::tempdir()?;
		let tmp_path = temp_dir.path().to_path_buf();

		let res: &str = with_current_dir_async(&tmp_path, || async {
			// Inside the async closure, the cwd should be the temp dir (canonicalized for macOS
			// /private symlink).
			let cwd = std::env::current_dir().unwrap();
			assert_eq!(cwd.canonicalize().unwrap(), tmp_path.canonicalize().unwrap());
			// Create a file relative to the new cwd to verify it's applied.
			fs::write("async.txt", b"ok").unwrap();
			Ok("async-done")
		})
		.await?;
		assert_eq!(res, "async-done");

		// After the closure, cwd should be restored.
		assert_eq!(std::env::current_dir()?, original);
		// The file should exist inside the temp dir.
		assert!(tmp_path.join("async.txt").exists());
		Ok(())
	}

	#[test]
	fn is_root_detects_root_user() {
		CommandMock::default()
			.with_command_script(
				"id",
				r#"#!/bin/sh
echo 0"#,
			)
			.execute_sync(|| {
				assert!(is_root());
			});
	}

	#[test]
	fn is_root_detects_non_root_user() {
		CommandMock::default()
			.with_command_script(
				"id",
				r#"#!/bin/sh
echo 1000"#,
			)
			.execute_sync(|| {
				assert!(!is_root());
			});
	}

	mod contract_artifact_tests {
		use super::*;
		use std::fs;

		#[test]
		fn find_contract_artifact_prefers_project_target() -> anyhow::Result<()> {
			let temp_dir = tempfile::tempdir()?;
			let workspace_root = temp_dir.path().join("workspace");
			let project_root = workspace_root.join("project");
			fs::create_dir_all(project_root.join("target").join("ink"))?;
			let project_artifact =
				project_root.join("target").join("ink").join("my_contract.contract");
			fs::write(&project_artifact, b"contract")?;

			fs::create_dir_all(workspace_root.join("target").join("ink"))?;
			let workspace_artifact =
				workspace_root.join("target").join("ink").join("my_contract.contract");
			fs::write(&workspace_artifact, b"workspace")?;
			fs::write(workspace_root.join("Cargo.toml"), "[workspace]\nmembers = [\"project\"]\n")?;
			fs::write(
				project_root.join("Cargo.toml"),
				"[package]\nname = \"my_contract\"\nversion = \"0.1.0\"\n",
			)?;

			let path = find_contract_artifact_path(&project_root, "my_contract")
				.expect("artifact should be found");
			assert_eq!(path, project_artifact);
			Ok(())
		}

		#[test]
		fn find_contract_artifact_falls_back_to_workspace() -> anyhow::Result<()> {
			let temp_dir = tempfile::tempdir()?;
			let workspace_root = temp_dir.path().join("workspace");
			let project_root = workspace_root.join("member");
			fs::create_dir_all(project_root.join("target").join("ink"))?;
			fs::create_dir_all(workspace_root.join("target").join("ink"))?;
			let workspace_artifact =
				workspace_root.join("target").join("ink").join("my_contract.contract");
			fs::write(&workspace_artifact, b"workspace")?;
			fs::write(workspace_root.join("Cargo.toml"), "[workspace]\nmembers = [\"member\"]\n")?;
			fs::write(
				project_root.join("Cargo.toml"),
				"[package]\nname = \"my_contract\"\nversion = \"0.1.0\"\n",
			)?;

			let path = find_contract_artifact_path(&project_root, "my_contract")
				.expect("artifact should be found");
			assert_eq!(path, workspace_artifact);
			Ok(())
		}

		#[test]
		fn find_contract_artifact_ignores_unrelated_workspace() -> anyhow::Result<()> {
			let temp_dir = tempfile::tempdir()?;
			let workspace_root = temp_dir.path().join("workspace");
			let project_root = workspace_root.join("member");
			fs::create_dir_all(project_root.join("target").join("ink"))?;
			fs::create_dir_all(workspace_root.join("target").join("ink"))?;
			let workspace_artifact =
				workspace_root.join("target").join("ink").join("my_contract.contract");
			fs::write(&workspace_artifact, b"workspace")?;
			fs::write(workspace_root.join("Cargo.toml"), "[workspace]\nmembers = [\"other\"]\n")?;
			fs::write(
				project_root.join("Cargo.toml"),
				"[package]\nname = \"my_contract\"\nversion = \"0.1.0\"\n",
			)?;

			let path = find_contract_artifact_path(&project_root, "my_contract");
			assert_eq!(path, None);
			Ok(())
		}
	}

	#[test]
	fn find_workspace_root_returns_workspace_dir() -> anyhow::Result<()> {
		let temp_dir = tempfile::tempdir()?;
		let workspace_root = temp_dir.path().join("workspace");
		let member_root = workspace_root.join("member");
		fs::create_dir_all(&member_root)?;
		fs::write(workspace_root.join("Cargo.toml"), "[workspace]\nmembers = [\"member\"]\n")?;
		fs::write(
			member_root.join("Cargo.toml"),
			"[package]\nname = \"member\"\nversion = \"0.1.0\"\n",
		)?;

		assert_eq!(find_workspace_root(&member_root), Some(workspace_root));
		Ok(())
	}

	#[test]
	fn find_workspace_root_returns_none_when_absent() -> anyhow::Result<()> {
		let temp_dir = tempfile::tempdir()?;
		let project_root = temp_dir.path().join("project");
		fs::create_dir_all(&project_root)?;
		fs::write(
			project_root.join("Cargo.toml"),
			"[package]\nname = \"project\"\nversion = \"0.1.0\"\n",
		)?;

		assert_eq!(find_workspace_root(&project_root), None);
		Ok(())
	}

	#[test]
	fn find_workspace_root_ignores_unrelated_workspace() -> anyhow::Result<()> {
		let temp_dir = tempfile::tempdir()?;
		let workspace_root = temp_dir.path().join("workspace");
		let member_root = workspace_root.join("member");
		fs::create_dir_all(&member_root)?;
		fs::write(workspace_root.join("Cargo.toml"), "[workspace]\nmembers = [\"other\"]\n")?;
		fs::write(
			member_root.join("Cargo.toml"),
			"[package]\nname = \"member\"\nversion = \"0.1.0\"\n",
		)?;

		assert_eq!(find_workspace_root(&member_root), None);
		Ok(())
	}
}