utpm 0.3.0

UTPM is a package manager for local and remote Typst packages. Quickly create and manage projects and templates on your system, and publish them directly to Typst Universe.
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/// Integration tests for UTPM commands
mod common;

use common::*;
use std::fs;

#[cfg(test)]
mod init_command_tests {
    use super::*;

    #[test]
    fn test_init_creates_manifest() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        let current_dir = temp_dir.path().join("current");
        fs::create_dir_all(&current_dir).unwrap();

        let manifest_path = current_dir.join("typst.toml");

        // Verify manifest doesn't exist yet
        assert!(!manifest_path.exists());

        // After init would run, manifest should exist
        // This is a placeholder for actual command execution

        cleanup_test_env();
    }

    #[test]
    fn test_init_with_force_flag() {
        let temp_dir = setup_temp_dir();
        let manifest_path = create_test_manifest(temp_dir.path(), "test", "1.0.0");

        // Verify manifest exists
        assert_file_exists(&manifest_path);

        let content_before = read_file_string(&manifest_path);

        // With --force flag, it should overwrite
        // This would require actual command execution

        assert!(content_before.contains("test"));
    }

    #[test]
    fn test_init_manifest_structure() {
        let temp_dir = setup_temp_dir();
        let manifest_path = create_test_manifest(temp_dir.path(), "my-package", "2.1.0");

        let content = read_file_string(&manifest_path);

        // Verify required fields
        assert!(content.contains("[package]"));
        assert!(content.contains("name = \"my-package\""));
        assert!(content.contains("version = \"2.1.0\""));
        assert!(content.contains("entrypoint = \"main.typ\""));
        assert!(content.contains("[tool.utpm]"));
    }

    #[test]
    fn test_init_with_custom_entrypoint() {
        let temp_dir = setup_temp_dir();
        let manifest_content = r#"[package]
name = "test"
version = "1.0.0"
entrypoint = "lib.typ"
authors = ["Test"]
license = "MIT"
description = "Test"

[tool.utpm]
namespace = "local"
"#;
        let manifest_path = create_custom_manifest(temp_dir.path(), manifest_content);

        let content = read_file_string(&manifest_path);
        assert!(content.contains("entrypoint = \"lib.typ\""));
    }

    #[test]
    fn test_init_populate_creates_files() {
        let temp_dir = setup_temp_dir();
        create_test_package(temp_dir.path(), "test-pkg", "1.0.0");

        // Verify files were created
        assert_file_exists(&temp_dir.path().join("typst.toml"));
        assert_file_exists(&temp_dir.path().join("main.typ"));
        assert_dir_exists(&temp_dir.path().join("src"));
        assert_dir_exists(&temp_dir.path().join("examples"));
    }
}

#[cfg(test)]
mod link_command_tests {
    use super::*;

    #[test]
    fn test_link_package_structure() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        // Create a test package
        let package_dir = temp_dir.path().join("test-package");
        create_test_package(&package_dir, "my-pkg", "1.0.0");

        // Verify package structure
        assert_file_exists(&package_dir.join("typst.toml"));
        assert_file_exists(&package_dir.join("main.typ"));

        cleanup_test_env();
    }

    #[test]
    fn test_link_target_directory() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        let data_dir = temp_dir.path().join("data/typst/packages");
        fs::create_dir_all(&data_dir).unwrap();

        // Link should create: data/typst/packages/local/my-pkg/1.0.0/
        let _expected_path = data_dir.join("local/my-pkg/1.0.0");

        assert_dir_exists(&data_dir);

        cleanup_test_env();
    }

    #[test]
    fn test_link_respects_gitignore() {
        let temp_dir = setup_temp_dir();
        let package_dir = temp_dir.path().join("package");
        fs::create_dir_all(&package_dir).unwrap();

        // Create .gitignore
        fs::write(package_dir.join(".gitignore"), "*.log\n.env\n").unwrap();

        // Create files
        fs::write(package_dir.join("main.typ"), "test").unwrap();
        fs::write(package_dir.join("debug.log"), "log").unwrap();
        fs::write(package_dir.join(".env"), "secret").unwrap();

        // Verify .gitignore exists
        assert_file_exists(&package_dir.join(".gitignore"));
    }

    #[test]
    fn test_link_with_exclude_patterns() {
        let temp_dir = setup_temp_dir();

        let manifest_content = r#"[package]
name = "test"
version = "1.0.0"
entrypoint = "main.typ"
authors = ["Test"]
license = "MIT"
description = "Test"

[tool.utpm]
namespace = "local"
exclude = [".git", "*.md", "tests/"]
"#;
        create_custom_manifest(temp_dir.path(), manifest_content);

        let content = read_file_string(&temp_dir.path().join("typst.toml"));
        assert!(content.contains("exclude = [\".git\", \"*.md\", \"tests/\"]"));
    }
}

#[cfg(test)]
mod clone_command_tests {
    use super::*;

    #[test]
    fn test_clone_package_format() {
        // Test valid package formats for cloning
        let valid_formats = vec![
            "@preview/example:1.0.0",
            "@preview/my-package:2.3.4",
            "@local/test:0.1.0",
        ];

        for format in valid_formats {
            assert!(format.starts_with('@'));
            assert!(format.contains('/'));
            assert!(format.contains(':'));
        }
    }

    #[test]
    fn test_clone_target_directory() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        // Preview packages go to cache
        let cache_dir = temp_dir.path().join("cache/typst/packages/preview");
        fs::create_dir_all(&cache_dir).unwrap();

        assert_dir_exists(&cache_dir);

        cleanup_test_env();
    }
}

#[cfg(test)]
mod unlink_command_tests {
    use super::*;

    #[test]
    fn test_unlink_removes_package() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        // Create a linked package
        let package_path = temp_dir
            .path()
            .join("data/typst/packages/local/test-pkg/1.0.0");
        fs::create_dir_all(&package_path).unwrap();
        fs::write(package_path.join("typst.toml"), "test").unwrap();

        assert_dir_exists(&package_path);

        // After unlink, directory should be removed
        // This would require actual command execution

        cleanup_test_env();
    }

    #[test]
    fn test_unlink_package_not_exist() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        let package_path = temp_dir
            .path()
            .join("data/typst/packages/local/nonexistent/1.0.0");

        assert_not_exists(&package_path);

        cleanup_test_env();
    }
}

#[cfg(test)]
mod bump_command_tests {
    use std::str::FromStr;

    use typst_syntax::package::PackageVersion;

    use super::*;

    #[test]
    fn test_bump_updates_version() {
        let temp_dir = setup_temp_dir();
        create_test_manifest(temp_dir.path(), "test", "1.0.0");

        let manifest_path = temp_dir.path().join("typst.toml");
        let content = read_file_string(&manifest_path);

        assert!(content.contains("version = \"1.0.0\""));

        // After bump to 1.1.0, should contain new version
        // This would require actual command execution
    }

    #[test]
    fn test_bump_version_formats() {
        // Test various version formats
        let versions = vec![
            ("1.0.0", "1.0.1"), // Patch
            ("1.0.0", "1.1.0"), // Minor
            ("1.0.0", "2.0.0"), // Major
            ("0.1.0", "0.1.1"), // Pre-release patch
        ];

        for (old, new) in versions {
            assert_ne!(old, new);
            // Verify semver parsing would work
            assert!(PackageVersion::from_str(old).is_ok());
            assert!(PackageVersion::from_str(new).is_ok());
        }
    }
}

#[cfg(test)]
mod sync_command_tests {
    use super::*;

    #[test]
    fn test_sync_finds_imports() {
        let temp_dir = setup_temp_dir();

        let typ_file = temp_dir.path().join("main.typ");
        let content = r#"#import "@preview/example:1.0.0": *
#import "@preview/other:2.3.4"

// Some code
"#;
        fs::write(&typ_file, content).unwrap();

        let file_content = read_file_string(&typ_file);
        assert!(file_content.contains("@preview/example:1.0.0"));
        assert!(file_content.contains("@preview/other:2.3.4"));
    }

    #[test]
    fn test_sync_check_mode() {
        // Test that --check mode doesn't modify files
        let temp_dir = setup_temp_dir();
        let typ_file = temp_dir.path().join("main.typ");
        let content = "#import \"@preview/example:1.0.0\": *";
        fs::write(&typ_file, content).unwrap();

        let before = read_file_string(&typ_file);

        // In check mode, file should remain unchanged
        let after = read_file_string(&typ_file);
        assert_eq!(before, after);
    }
}

#[cfg(test)]
mod list_command_tests {
    use super::*;

    #[test]
    fn test_list_local_packages() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        let data_dir = temp_dir.path().join("data/typst/packages");

        // Create some test packages
        let pkg1 = data_dir.join("local/package1/1.0.0");
        let pkg2 = data_dir.join("local/package2/2.1.0");

        fs::create_dir_all(&pkg1).unwrap();
        fs::create_dir_all(&pkg2).unwrap();

        assert_dir_exists(&pkg1);
        assert_dir_exists(&pkg2);

        cleanup_test_env();
    }

    #[test]
    fn test_list_multiple_versions() {
        let temp_dir = setup_temp_dir();
        setup_test_env(temp_dir.path());

        let data_dir = temp_dir.path().join("data/typst/packages/local/package");

        // Create multiple versions
        fs::create_dir_all(data_dir.join("1.0.0")).unwrap();
        fs::create_dir_all(data_dir.join("1.1.0")).unwrap();
        fs::create_dir_all(data_dir.join("2.0.0")).unwrap();

        let versions: Vec<_> = fs::read_dir(&data_dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .collect();

        assert_eq!(versions.len(), 3);
        assert!(versions.iter().any(|v| v == "1.0.0"));
        assert!(versions.iter().any(|v| v == "2.0.0"));

        cleanup_test_env();
    }
}

#[cfg(test)]
mod metadata_command_tests {
    use super::*;

    #[test]
    fn test_metadata_extracts_all_fields() {
        let temp_dir = setup_temp_dir();
        create_test_manifest(temp_dir.path(), "test-pkg", "3.2.1");

        let content = read_file_string(&temp_dir.path().join("typst.toml"));

        // Verify all standard fields are present
        assert!(content.contains("name"));
        assert!(content.contains("version"));
        assert!(content.contains("entrypoint"));
        assert!(content.contains("authors"));
        assert!(content.contains("license"));
        assert!(content.contains("description"));
    }

    #[test]
    fn test_metadata_specific_field() {
        let temp_dir = setup_temp_dir();
        create_test_manifest(temp_dir.path(), "my-package", "1.2.3");

        let content = read_file_string(&temp_dir.path().join("typst.toml"));

        // Extract specific fields
        assert!(content.contains("name = \"my-package\""));
        assert!(content.contains("version = \"1.2.3\""));
    }
}

#[cfg(test)]
mod get_command_tests {
    #[test]
    fn test_get_package_info() {
        // Test package info retrieval concepts
        let package_name = "example";
        let package_version = "1.0.0";

        assert!(!package_name.is_empty());
        assert!(!package_version.is_empty());
    }
}

#[cfg(test)]
mod install_command_tests {
    use super::*;

    #[test]
    fn test_install_from_git() {
        // Test git URL validation
        let valid_git_urls = vec![
            "https://github.com/user/repo.git",
            "git@github.com:user/repo.git",
            "git://github.com/user/repo.git",
        ];

        for url in valid_git_urls {
            assert!(url.contains("git") || url.starts_with("https"));
        }
    }

    #[test]
    fn test_install_dependencies() {
        let temp_dir = setup_temp_dir();

        let manifest_content = r#"[package]
name = "test"
version = "1.0.0"
entrypoint = "main.typ"
authors = ["Test"]
license = "MIT"
description = "Test"

[tool.utpm]
namespace = "local"
dependencies = [
    "https://github.com/example/dep1.git",
    "https://github.com/example/dep2.git"
]
"#;
        create_custom_manifest(temp_dir.path(), manifest_content);

        let content = read_file_string(&temp_dir.path().join("typst.toml"));
        assert!(content.contains("dependencies"));
    }
}