xref: /DADK/dadk/src/console/tests.rs (revision ce26d6757b4911f3f1632cdae52a89574d7e9a67)
1 use rootfs::CreateCommandParam;
2 use user::UserCleanLevel;
3 
4 use super::*;
5 
6 #[test]
test_command_line_args_default()7 fn test_command_line_args_default() {
8     let args = CommandLineArgs::parse_from(&["dadk", "kernel"]);
9     assert_eq!(args.action, Action::Kernel);
10     assert_eq!(args.manifest_path, "dadk-manifest.toml");
11 }
12 
13 #[test]
test_command_line_args_with_manifest()14 fn test_command_line_args_with_manifest() {
15     // test short
16     let args = CommandLineArgs::parse_from(&["dadk", "-f", "custom-manifest.toml", "kernel"]);
17     assert_eq!(args.action, Action::Kernel);
18     assert_eq!(args.manifest_path, "custom-manifest.toml");
19     // test long
20     let args =
21         CommandLineArgs::parse_from(&["dadk", "--manifest", "custom-manifest.toml", "kernel"]);
22     assert_eq!(args.action, Action::Kernel);
23     assert_eq!(args.manifest_path, "custom-manifest.toml");
24 }
25 
26 #[test]
test_command_line_args_rootfs_subcommand()27 fn test_command_line_args_rootfs_subcommand() {
28     let args = CommandLineArgs::parse_from(&["dadk", "rootfs", "create"]);
29     assert!(matches!(
30         args.action,
31         Action::Rootfs(RootFSCommand::Create(CreateCommandParam {
32             skip_if_exists: false
33         }))
34     ));
35 
36     let args = CommandLineArgs::parse_from(&["dadk", "rootfs", "create", "--skip-if-exists"]);
37     assert!(matches!(
38         args.action,
39         Action::Rootfs(RootFSCommand::Create(CreateCommandParam {
40             skip_if_exists: true
41         }))
42     ));
43 }
44 
45 #[test]
test_show_mountpoint()46 fn test_show_mountpoint() {
47     let args = CommandLineArgs::parse_from(&["dadk", "rootfs", "show-mountpoint"]);
48     assert!(matches!(
49         args.action,
50         Action::Rootfs(RootFSCommand::ShowMountPoint)
51     ));
52 }
53 
54 #[test]
test_command_line_args_user()55 fn test_command_line_args_user() {
56     let args = CommandLineArgs::parse_from(&["dadk", "user", "build"]);
57 
58     assert!(matches!(args.action, Action::User(UserCommand::Build)));
59 }
60 
61 /// 该函数测试CommandLineArgs解析器是否正确解析`dadk user clean`命令
62 #[test]
test_command_line_args_user_clean()63 fn test_command_line_args_user_clean() {
64     let args = CommandLineArgs::parse_from(&["dadk", "user", "clean"]);
65     assert!(matches!(args.action, Action::User(UserCommand::Clean(_))));
66     if let Action::User(UserCommand::Clean(args)) = args.action {
67         assert_eq!(args.level, UserCleanLevel::All);
68     } else {
69         panic!("Expected UserCommand::Clean");
70     }
71 
72     // 检查 `--level` 参数
73     let args = CommandLineArgs::parse_from(&["dadk", "user", "clean", "--level", "in-src"]);
74     if let Action::User(UserCommand::Clean(args)) = args.action {
75         assert_eq!(args.level, UserCleanLevel::InSrc);
76     } else {
77         panic!("Expected UserCommand::Clean");
78     }
79 
80     // 检查 `--task` 参数
81     let args = CommandLineArgs::parse_from(&["dadk", "user", "clean", "--task", "a-0.1.0"]);
82     if let Action::User(UserCommand::Clean(args)) = args.action {
83         assert_eq!(args.task, Some("a-0.1.0".to_string()));
84     } else {
85         panic!("Expected UserCommand::Clean");
86     }
87 }
88