update_version/parsers/
tauri_config_parser.rs1use crate::parsers::package_json_parser::PackageJsonParser;
2use crate::parsers::Parser;
3use regex::Regex;
4use semver::Version;
5
6pub struct TauriConfigParser;
7
8impl Parser for TauriConfigParser {
9 fn version_match_regex() -> anyhow::Result<Regex> {
10 PackageJsonParser::version_match_regex()
11 }
12
13 fn filename_match_regex() -> anyhow::Result<Regex> {
14 Ok(Regex::new(r#"(?i)[/\\]tauri\.conf\.json$"#)?)
15 }
16
17 fn version_line_format(version: &Version) -> anyhow::Result<String> {
18 Ok(format!(
19 "${{1}}{}.{}.{}\"",
20 version.major, version.minor, version.patch
21 ))
22 }
23}
24
25#[cfg(test)]
26mod tests {
27 use super::*;
28
29 #[test]
30 fn test_version_regex_matches_in_tauri_config() {
31 let regex = TauriConfigParser::version_match_regex().unwrap();
32 let content = r#"{
33 "productName": "My App",
34 "version": "1.0.0",
35 "identifier": "com.example.app"
36}"#;
37 let captures = regex.captures(content).unwrap();
38 assert_eq!(captures.get(2).unwrap().as_str(), "1.0.0");
39 }
40
41 #[test]
42 fn test_filename_regex_matches_tauri_conf_json() {
43 let regex = TauriConfigParser::filename_match_regex().unwrap();
44 assert!(regex.is_match("/path/to/tauri.conf.json"));
45 assert!(regex.is_match("\\path\\to\\tauri.conf.json"));
46 assert!(regex.is_match("/src-tauri/tauri.conf.json"));
47 }
48
49 #[test]
50 fn test_filename_regex_case_insensitive() {
51 let regex = TauriConfigParser::filename_match_regex().unwrap();
52 assert!(regex.is_match("/path/to/TAURI.CONF.JSON"));
53 assert!(regex.is_match("/path/to/Tauri.Conf.Json"));
54 }
55
56 #[test]
57 fn test_filename_regex_no_false_positives() {
58 let regex = TauriConfigParser::filename_match_regex().unwrap();
59 assert!(!regex.is_match("/path/to/tauri.conf.json.bak"));
60 assert!(!regex.is_match("/path/to/my-tauri.conf.json"));
61 assert!(!regex.is_match("/path/to/tauri.json"));
62 }
63
64 #[test]
65 fn test_version_line_format_strips_prerelease() {
66 let version = Version::parse("1.2.3-beta.1").unwrap();
68 let formatted = TauriConfigParser::version_line_format(&version).unwrap();
69 assert_eq!(formatted, "${1}1.2.3\"");
70 }
71
72 #[test]
73 fn test_version_line_format_simple() {
74 let version = Version::parse("2.0.0").unwrap();
75 let formatted = TauriConfigParser::version_line_format(&version).unwrap();
76 assert_eq!(formatted, "${1}2.0.0\"");
77 }
78}