Skip to content

Commit 9fb7f8c

Browse files
committed
chore: 更新包管理器逻辑以支持新结构
- 在 `Cargo.toml` 中添加 `strum` 依赖以支持枚举字符串解析。 - 重构 `PM` 枚举为 `PackageManager` 结构,简化包管理器的处理逻辑。 - 移除 `pm_metadata.rs` 文件,整合包管理器元数据的解析逻辑。 - 更新相关测试用例以适应新的包管理器实现,确保功能一致性和代码整洁性。
1 parent 7c7a78a commit 9fb7f8c

File tree

9 files changed

+120
-178
lines changed

9 files changed

+120
-178
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ indicatif = "0.17.8"
107107
flate2 = "1.0.28"
108108
glob = "0.3.2"
109109
once_cell = "1.20.2"
110+
strum = { version = "0.27.1", features = ["derive"] }
110111
tar = "0.4.40"
111112
tempfile = "=3.16.0"
112113
uuid = "=1.12.1"

crates/snm_pm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ robust_downloader = { workspace = true }
1818
semver = { workspace = true }
1919
serde = { workspace = true }
2020
serde_json = { workspace = true }
21+
strum = { workspace = true }
2122
tokio = { workspace = true }
2223
tracing = { workspace = true }
2324
up_finder = { workspace = true }
2425

25-
2626
# self
2727
snm_config = { workspace = true }
2828
snm_downloader = { workspace = true }

crates/snm_pm/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod ops;
22
pub mod package_json;
33
pub mod pm;
4-
pub mod pm_metadata;

crates/snm_pm/src/ops/npm.rs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ impl PackageManagerOps for NpmCommandLine {
9696
#[cfg(test)]
9797
mod tests {
9898

99+
use std::str::FromStr;
100+
99101
use super::*;
100-
use crate::{ops::ops::RunArgs, pm::PM};
102+
use crate::{ops::ops::RunArgs, pm::PackageManager};
101103

102104
#[tokio::test]
103105
async fn should_parse_npm_command() -> anyhow::Result<()> {
104-
let pm = PM::parse("npm@8.0.0")?;
106+
let pm = PackageManager::from_str("npm@8.0.0")?;
105107

106108
let ops = pm.get_ops();
107109

@@ -122,7 +124,7 @@ mod tests {
122124

123125
#[tokio::test]
124126
async fn should_parse_npm_command_with_frozen() -> anyhow::Result<()> {
125-
let pm = PM::parse("npm@8.0.0")?;
127+
let pm = PackageManager::from_str("npm@8.0.0")?;
126128

127129
let ops = pm.get_ops();
128130

@@ -143,7 +145,7 @@ mod tests {
143145

144146
#[tokio::test]
145147
async fn should_parse_npm_command_with_save_prod() -> anyhow::Result<()> {
146-
let pm = PM::parse("npm@8.0.0")?;
148+
let pm = PackageManager::from_str("npm@8.0.0")?;
147149

148150
let ops = pm.get_ops();
149151

@@ -164,7 +166,7 @@ mod tests {
164166

165167
#[tokio::test]
166168
async fn should_parse_npm_command_with_save_peer() -> anyhow::Result<()> {
167-
let pm = PM::parse("npm@8.0.0")?;
169+
let pm = PackageManager::from_str("npm@8.0.0")?;
168170

169171
let ops = pm.get_ops();
170172

@@ -185,7 +187,7 @@ mod tests {
185187

186188
#[tokio::test]
187189
async fn should_parse_npm_command_with_save_dev() -> anyhow::Result<()> {
188-
let pm = PM::parse("npm@8.0.0")?;
190+
let pm = PackageManager::from_str("npm@8.0.0")?;
189191

190192
let ops = pm.get_ops();
191193

@@ -206,7 +208,7 @@ mod tests {
206208

207209
#[tokio::test]
208210
async fn should_parse_npm_command_with_save_optional() -> anyhow::Result<()> {
209-
let pm = PM::parse("npm@8.0.0")?;
211+
let pm = PackageManager::from_str("npm@8.0.0")?;
210212

211213
let ops = pm.get_ops();
212214

@@ -227,7 +229,7 @@ mod tests {
227229

228230
#[tokio::test]
229231
async fn should_parse_npm_command_with_save_exact() -> anyhow::Result<()> {
230-
let pm = PM::parse("npm@8.0.0")?;
232+
let pm = PackageManager::from_str("npm@8.0.0")?;
231233

232234
let ops = pm.get_ops();
233235

@@ -248,7 +250,7 @@ mod tests {
248250

249251
#[tokio::test]
250252
async fn should_parse_npm_command_with_run() -> anyhow::Result<()> {
251-
let pm = PM::parse("npm@8.0.0")?;
253+
let pm = PackageManager::from_str("npm@8.0.0")?;
252254

253255
let ops = pm.get_ops();
254256

@@ -264,7 +266,7 @@ mod tests {
264266

265267
#[tokio::test]
266268
async fn should_parse_npm_command_with_run_with_passthrough_args() -> anyhow::Result<()> {
267-
let pm = PM::parse("npm@8.0.0")?;
269+
let pm = PackageManager::from_str("npm@8.0.0")?;
268270

269271
let ops = pm.get_ops();
270272

@@ -280,7 +282,7 @@ mod tests {
280282

281283
#[tokio::test]
282284
async fn should_fail_when_save_peer_and_optional_are_set() -> anyhow::Result<()> {
283-
let pm = PM::parse("npm@8.0.0")?;
285+
let pm = PackageManager::from_str("npm@8.0.0")?;
284286
let ops = pm.get_ops();
285287

286288
let result = ops.install(InstallArgs {
@@ -297,23 +299,9 @@ mod tests {
297299
Ok(())
298300
}
299301

300-
// #[tokio::test]
301-
// async fn should_parse_npm_command_with_empty_command() -> anyhow::Result<()> {
302-
// let pm = PM::parse("npm@8.0.0")?;
303-
// let ops = pm.get_ops();
304-
305-
// let result = ops.run(RunArgs {
306-
// command: "".to_string(),
307-
// passthrough_args: vec!["--foo".to_string(), "--bar".to_string()],
308-
// });
309-
310-
// assert!(result.is_err());
311-
// Ok(())
312-
// }
313-
314302
#[tokio::test]
315303
async fn should_parse_npm_command_with_remove_multiple_packages() -> anyhow::Result<()> {
316-
let pm = PM::parse("npm@8.0.0")?;
304+
let pm = PackageManager::from_str("npm@8.0.0")?;
317305
let ops = pm.get_ops();
318306

319307
let cmd = ops.remove(RemoveArgs {
@@ -327,7 +315,7 @@ mod tests {
327315
#[tokio::test]
328316
async fn should_parse_npm_command_with_special_characters_in_package_spec() -> anyhow::Result<()>
329317
{
330-
let pm = PM::parse("npm@8.0.0")?;
318+
let pm = PackageManager::from_str("npm@8.0.0")?;
331319
let ops = pm.get_ops();
332320

333321
let cmd = ops.install(InstallArgs {

crates/snm_pm/src/ops/pnpm.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,14 @@ impl PackageManagerOps for PnpmCommandLine {
9191

9292
#[cfg(test)]
9393
mod tests {
94+
use std::str::FromStr;
95+
9496
use super::*;
95-
use crate::{ops::ops::RunArgs, pm::PM};
97+
use crate::{ops::ops::RunArgs, pm::PackageManager};
9698

9799
#[tokio::test]
98100
async fn should_parse_pnpm_command() -> anyhow::Result<()> {
99-
let pm = PM::parse("pnpm@8.0.0")?;
101+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
100102
let ops = pm.get_ops();
101103

102104
let cmd = ops.install(InstallArgs {
@@ -115,7 +117,7 @@ mod tests {
115117

116118
#[tokio::test]
117119
async fn should_parse_pnpm_command_with_frozen() -> anyhow::Result<()> {
118-
let pm = PM::parse("pnpm@8.0.0")?;
120+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
119121
let ops = pm.get_ops();
120122

121123
let cmd = ops.install(InstallArgs {
@@ -134,7 +136,7 @@ mod tests {
134136

135137
#[tokio::test]
136138
async fn should_parse_pnpm_command_with_save_prod() -> anyhow::Result<()> {
137-
let pm = PM::parse("pnpm@8.0.0")?;
139+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
138140
let ops = pm.get_ops();
139141

140142
let cmd = ops.install(InstallArgs {
@@ -153,7 +155,7 @@ mod tests {
153155

154156
#[tokio::test]
155157
async fn should_parse_pnpm_command_with_save_peer() -> anyhow::Result<()> {
156-
let pm = PM::parse("pnpm@8.0.0")?;
158+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
157159
let ops = pm.get_ops();
158160

159161
let cmd = ops.install(InstallArgs {
@@ -172,7 +174,7 @@ mod tests {
172174

173175
#[tokio::test]
174176
async fn should_parse_pnpm_command_with_save_dev() -> anyhow::Result<()> {
175-
let pm = PM::parse("pnpm@8.0.0")?;
177+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
176178
let ops = pm.get_ops();
177179

178180
let cmd = ops.install(InstallArgs {
@@ -191,7 +193,7 @@ mod tests {
191193

192194
#[tokio::test]
193195
async fn should_parse_pnpm_command_with_save_optional() -> anyhow::Result<()> {
194-
let pm = PM::parse("pnpm@8.0.0")?;
196+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
195197
let ops = pm.get_ops();
196198

197199
let cmd = ops.install(InstallArgs {
@@ -210,7 +212,7 @@ mod tests {
210212

211213
#[tokio::test]
212214
async fn should_parse_pnpm_command_with_save_exact() -> anyhow::Result<()> {
213-
let pm = PM::parse("pnpm@8.0.0")?;
215+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
214216
let ops = pm.get_ops();
215217

216218
let cmd = ops.install(InstallArgs {
@@ -229,7 +231,7 @@ mod tests {
229231

230232
#[tokio::test]
231233
async fn should_parse_pnpm_command_with_run() -> anyhow::Result<()> {
232-
let pm = PM::parse("pnpm@8.0.0")?;
234+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
233235
let ops = pm.get_ops();
234236

235237
let cmd = ops.run(RunArgs {
@@ -243,7 +245,7 @@ mod tests {
243245

244246
#[tokio::test]
245247
async fn should_parse_pnpm_command_with_run_with_passthrough_args() -> anyhow::Result<()> {
246-
let pm = PM::parse("pnpm@8.0.0")?;
248+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
247249
let ops = pm.get_ops();
248250

249251
let cmd = ops.run(RunArgs {
@@ -257,7 +259,7 @@ mod tests {
257259

258260
#[tokio::test]
259261
async fn should_fail_when_save_peer_and_optional_are_set() -> anyhow::Result<()> {
260-
let pm = PM::parse("pnpm@8.0.0")?;
262+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
261263
let ops = pm.get_ops();
262264

263265
let result = ops.install(InstallArgs {
@@ -276,7 +278,7 @@ mod tests {
276278

277279
// #[tokio::test]
278280
// async fn should_parse_pnpm_command_with_empty_command() -> anyhow::Result<()> {
279-
// let pm = PM::parse("pnpm@8.0.0")?;
281+
// let pm = PackageManager::from_str("pnpm@8.0.0")?;
280282
// let ops = pm.get_ops();
281283

282284
// let result = ops.run(RunArgs {
@@ -290,7 +292,7 @@ mod tests {
290292

291293
#[tokio::test]
292294
async fn should_parse_pnpm_command_with_remove_multiple_packages() -> anyhow::Result<()> {
293-
let pm = PM::parse("pnpm@8.0.0")?;
295+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
294296
let ops = pm.get_ops();
295297

296298
let cmd = ops.remove(RemoveArgs {
@@ -304,7 +306,7 @@ mod tests {
304306
#[tokio::test]
305307
async fn should_parse_pnpm_command_with_special_characters_in_package_spec() -> anyhow::Result<()>
306308
{
307-
let pm = PM::parse("pnpm@8.0.0")?;
309+
let pm = PackageManager::from_str("pnpm@8.0.0")?;
308310
let ops = pm.get_ops();
309311

310312
let cmd = ops.install(InstallArgs {

0 commit comments

Comments
 (0)