From 13d01866cd980ca1cda5ada4ecf6b7cadc7c9344 Mon Sep 17 00:00:00 2001 From: Alex <49969959+alexzhang1030@users.noreply.github.com> Date: Thu, 10 Aug 2023 11:58:17 +0800 Subject: [PATCH] refactor(crate/package_json): extract duplicate (#68) --- Cargo.lock | 1 + crates/package_json/Cargo.toml | 1 + crates/package_json/src/lib.rs | 40 +++++++++++++------ ...son__tests__init_package_json_content.snap | 16 ++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 crates/package_json/src/snapshots/pacquet_package_json__tests__init_package_json_content.snap diff --git a/Cargo.lock b/Cargo.lock index b430af4ab..d6e506301 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1272,6 +1272,7 @@ dependencies = [ name = "pacquet_package_json" version = "0.0.1" dependencies = [ + "insta", "pacquet_diagnostics", "serde", "serde_json", diff --git a/crates/package_json/Cargo.toml b/crates/package_json/Cargo.toml index f98baac55..a895d26bb 100644 --- a/crates/package_json/Cargo.toml +++ b/crates/package_json/Cargo.toml @@ -19,3 +19,4 @@ strum = { workspace = true } [dev-dependencies] tempfile = { workspace = true } +insta = { workspace = true } diff --git a/crates/package_json/src/lib.rs b/crates/package_json/src/lib.rs index d8cc3b1fc..efcc03482 100644 --- a/crates/package_json/src/lib.rs +++ b/crates/package_json/src/lib.rs @@ -68,26 +68,35 @@ impl PackageJson { PackageJson { path, value } } - fn write_to_file(path: &PathBuf) -> Result { - let mut file = fs::File::create(path)?; - let name = path - .parent() - .and_then(|folder| folder.file_name()) - .and_then(|file_name| file_name.to_str()) - .unwrap_or(""); + fn get_init_package_json(name: &str) -> Result { let package_json = json!({ "name": name, "version": "1.0.0", "description": "", "main": "index.js", - "script": { + "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }); - let contents = serde_json::to_string_pretty(&package_json)?; + Ok(package_json) + } + + fn to_string_prettify(package_json: &Value) -> Result { + Ok(serde_json::to_string_pretty(package_json)?) + } + + fn write_to_file(path: &PathBuf) -> Result { + let mut file = fs::File::create(path)?; + let name = path + .parent() + .and_then(|folder| folder.file_name()) + .and_then(|file_name| file_name.to_str()) + .unwrap_or(""); + let package_json = PackageJson::get_init_package_json(name)?; + let contents = PackageJson::to_string_prettify(&package_json)?; file.write_all(contents.as_bytes())?; Ok(package_json) } @@ -107,7 +116,7 @@ impl PackageJson { println!( "Wrote to {}\n\n{}", path.display(), - serde_json::to_string_pretty(&package_json).unwrap_or(String::new()) + PackageJson::to_string_prettify(&package_json).unwrap_or(String::new()) ); Ok(()) } @@ -132,7 +141,7 @@ impl PackageJson { pub fn save(&mut self) -> Result<(), PackageJsonError> { let mut file = fs::File::create(&self.path)?; - let contents = serde_json::to_string_pretty(&self.value)?; + let contents = PackageJson::to_string_prettify(&self.value)?; file.write_all(contents.as_bytes())?; Ok(()) } @@ -205,11 +214,18 @@ impl PackageJson { mod tests { use std::fs::read_to_string; + use insta::assert_snapshot; use tempfile::{tempdir, NamedTempFile}; use super::*; use crate::DependencyGroup; + #[test] + fn test_init_package_json_content() { + let package_json = PackageJson::get_init_package_json("test").unwrap(); + assert_snapshot!(PackageJson::to_string_prettify(&package_json).unwrap_or(String::new())); + } + #[test] fn test_dependency_group_into() { assert_eq!(>::into(DependencyGroup::Default), "dependencies"); @@ -264,7 +280,7 @@ mod tests { let dir = tempdir().unwrap(); let tmp = dir.path().join("package.json"); let package_json = PackageJson::create_if_needed(&tmp).unwrap(); - package_json.get_script("test", false).expect_err("test command should not exist"); + package_json.get_script("dev", false).expect_err("dev command should not exist"); } #[test] diff --git a/crates/package_json/src/snapshots/pacquet_package_json__tests__init_package_json_content.snap b/crates/package_json/src/snapshots/pacquet_package_json__tests__init_package_json_content.snap new file mode 100644 index 000000000..2d6756665 --- /dev/null +++ b/crates/package_json/src/snapshots/pacquet_package_json__tests__init_package_json_content.snap @@ -0,0 +1,16 @@ +--- +source: crates/package_json/src/lib.rs +expression: "PackageJson::to_string_prettify(&package_json).unwrap_or(String::new())" +--- +{ + "name": "test", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +}