Skip to content

Commit 865790a

Browse files
feat: move print to main file (#1)
1 parent b8bc018 commit 865790a

File tree

3 files changed

+120
-106
lines changed

3 files changed

+120
-106
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# cargo-semver
22

3+
[![codecov](https://codecov.io/gh/filipstefansson/cargo-semver/branch/master/graph/badge.svg?token=HSAldVxPvX)](https://codecov.io/gh/filipstefansson/cargo-semver)
4+
35
**cargo-semver** is a cargo subcommand to help you update the version in your `Cargo.toml` file.
46

57
- Update the version with `cargo semver major|minor|patch|pre`

src/main.rs

Lines changed: 112 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use seahorse::{App, Command, Context, Flag, FlagType};
55
use std::env;
66
use version::{Bump, Version};
77

8+
#[cfg(not(tarpaulin_include))]
89
fn main() {
910
let args: Vec<String> = env::args().collect();
1011

@@ -24,7 +25,7 @@ fn main() {
2425
Command::new("set")
2526
.description("Set the version in Cargo.toml")
2627
.usage("cargo-semver set [VERSION]")
27-
.action(set_version_action)
28+
.action(set_action)
2829
.flag(config_file_flag.clone()),
2930
)
3031
.command(
@@ -70,52 +71,149 @@ fn default_action(c: &Context) {
7071
println!("{}", version.version);
7172
}
7273

73-
/// Set the version in `Config.toml` to the `VALUE` input
74-
/// in `cargo-semver set [VERSION]`.
75-
fn set_version_action(c: &Context) {
74+
fn set_version(c: &Context, version_arg: &str) -> String {
7675
let mut version = Version::new(c);
7776

78-
// the new version argument
79-
let version_arg = c.args.join(" ");
80-
81-
let new_version = match version_arg.as_str() {
77+
let new_version = match version_arg {
8278
v => match semver::Version::parse(&v) {
8379
Ok(v) => v,
8480
Err(err) => panic!("{}", err),
8581
},
8682
};
8783

88-
version.set(new_version);
84+
let new_version = version.set(new_version);
85+
println!("{}", new_version);
86+
new_version
8987
}
9088

91-
fn bump_version(c: &Context, bump: Bump) {
89+
fn bump_version(c: &Context, bump: Bump) -> String {
9290
let mut version = Version::new(c);
9391

9492
let pre_flag = match c.string_flag("pre") {
9593
Ok(flag) => Some(flag),
9694
_ => None,
9795
};
9896

99-
version.bump(bump, pre_flag)
97+
let new_version = version.bump(bump, pre_flag);
98+
println!("{}", new_version);
99+
new_version
100+
}
101+
102+
/// Set the version in `Config.toml` to the `VALUE` input
103+
/// in `cargo-semver set [VERSION]`.
104+
fn set_action(c: &Context) {
105+
// the new version argument
106+
let version_arg = c.args.join(" ");
107+
108+
set_version(c, &version_arg);
100109
}
101110

102111
/// Increments the patch version number.
103112
fn patch_action(c: &Context) {
104-
bump_version(c, Bump::Patch)
113+
bump_version(c, Bump::Patch);
105114
}
106115

107116
/// Increments the minor version number.
108117
fn minor_action(c: &Context) {
109-
bump_version(c, Bump::Minor)
118+
bump_version(c, Bump::Minor);
110119
}
111120

112121
/// Increments the major version number.
113122
fn major_action(c: &Context) {
114-
bump_version(c, Bump::Major)
123+
bump_version(c, Bump::Major);
115124
}
116125

117126
/// Increments the pre-release version number.
118127
fn pre_action(c: &Context) {
119128
let value = c.args.join(" ");
120129
bump_version(c, Bump::Pre(value));
121130
}
131+
132+
#[cfg(test)]
133+
mod tests {
134+
use super::*;
135+
use predicates::prelude::*;
136+
use seahorse::{Context, Flag, FlagType};
137+
use std::{fs, io::Write};
138+
use tempfile::NamedTempFile;
139+
140+
fn setup_test_context(
141+
file: &mut NamedTempFile,
142+
version: &str,
143+
extra_args: &mut Vec<String>,
144+
) -> Context {
145+
let file_has_content = file.as_file().metadata().unwrap().len() > 0;
146+
if !file_has_content {
147+
writeln!(
148+
file,
149+
"[package]\nversion = \"{}\"\n\n[dependencies]\nversion = \"{}\"",
150+
version, version,
151+
)
152+
.unwrap();
153+
}
154+
155+
let config_path = file.path().to_str().unwrap().to_string();
156+
let config_flag = Flag::new("config", FlagType::String);
157+
let pre_flag = Flag::new("pre", FlagType::String);
158+
159+
let mut args = vec!["--config".to_string(), config_path];
160+
args.append(extra_args);
161+
162+
Context::new(args, Some(vec![config_flag, pre_flag]), "".to_string())
163+
}
164+
165+
fn get_file_path(file: &mut NamedTempFile) -> String {
166+
file.path().to_str().unwrap().to_string()
167+
}
168+
169+
#[test]
170+
fn test_bump_version() {
171+
let mut file = NamedTempFile::new().unwrap();
172+
let context = setup_test_context(&mut file, "1.0.0", &mut vec![]);
173+
let path = get_file_path(&mut file);
174+
175+
major_action(&context);
176+
let contains = predicate::str::contains("2.0.0");
177+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
178+
179+
minor_action(&context);
180+
let contains = predicate::str::contains("2.1.0");
181+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
182+
183+
patch_action(&context);
184+
let contains = predicate::str::contains("2.1.1");
185+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
186+
187+
let new_context = setup_test_context(&mut file, "0", &mut vec!["alpha".to_string()]);
188+
pre_action(&new_context);
189+
let contains = predicate::str::contains("2.1.1-alpha.1");
190+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
191+
192+
pre_action(&new_context);
193+
let contains = predicate::str::contains("2.1.1-alpha.2");
194+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
195+
196+
let new_context = setup_test_context(&mut file, "0", &mut vec!["beta".to_string()]);
197+
pre_action(&new_context);
198+
let contains = predicate::str::contains("2.1.1-beta.1");
199+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
200+
201+
let new_context = setup_test_context(
202+
&mut file,
203+
"0",
204+
&mut vec!["--pre".to_string(), "beta".to_string()],
205+
);
206+
major_action(&new_context);
207+
let contains = predicate::str::contains("3.0.0-beta.1");
208+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
209+
210+
major_action(&context);
211+
let contains = predicate::str::contains("4.0.0");
212+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
213+
214+
let new_context = setup_test_context(&mut file, "0", &mut vec!["1.5.0".to_string()]);
215+
set_action(&new_context);
216+
let contains = predicate::str::contains("1.5.0");
217+
assert_eq!(true, contains.eval(&fs::read_to_string(&path).unwrap()));
218+
}
219+
}

src/version.rs

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ impl Version {
6363
panic!("failed to find version in Config.toml")
6464
}
6565

66-
pub fn set(&mut self, version: semver::Version) {
66+
pub fn set(&mut self, version: semver::Version) -> String {
6767
self.version = version;
6868

69-
self.update_config_version();
69+
self.update_config_version()
7070
}
7171

72-
pub fn bump(&mut self, bump: Bump, pre_flag: Option<String>) {
72+
pub fn bump(&mut self, bump: Bump, pre_flag: Option<String>) -> String {
7373
match bump {
7474
Bump::Major => &self.version.increment_major(),
7575
Bump::Minor => &self.version.increment_minor(),
@@ -81,10 +81,10 @@ impl Version {
8181
self.version.pre = vec![Identifier::AlphaNumeric(pre_flag), Identifier::Numeric(1)];
8282
}
8383

84-
self.update_config_version();
84+
self.update_config_version()
8585
}
8686

87-
fn update_config_version(&self) {
87+
fn update_config_version(&self) -> String {
8888
// validate version
8989
let string_version = &self.version.to_string();
9090
if let Err(err) = semver::Version::parse(string_version) {
@@ -105,7 +105,7 @@ impl Version {
105105
.output()
106106
.expect("failed to run `cargo check` to update Cargo.lock");
107107

108-
println!("{}", &self.version);
108+
self.version.to_string()
109109
}
110110

111111
fn increment_pre(&mut self, pre_version: String) {
@@ -139,89 +139,3 @@ impl Version {
139139
}
140140
}
141141
}
142-
143-
#[cfg(test)]
144-
mod tests {
145-
use super::*;
146-
use seahorse::{Context, Flag, FlagType};
147-
use std::io::Write;
148-
use tempfile::NamedTempFile;
149-
150-
fn setup_test_context(file: &mut NamedTempFile, version: &str) -> Context {
151-
writeln!(
152-
file,
153-
"[package]\nversion = \"{}\"\n\n[dependencies]\nversion = \"{}\"",
154-
version, version,
155-
)
156-
.unwrap();
157-
let config_path = file.path().to_str().unwrap().to_string();
158-
159-
let config_flag = Flag::new("config", FlagType::String);
160-
161-
Context::new(
162-
vec!["--config".to_string(), config_path],
163-
Some(vec![config_flag]),
164-
"".to_string(),
165-
)
166-
}
167-
168-
#[test]
169-
fn test_create_version() {
170-
let mut file = NamedTempFile::new().unwrap();
171-
let context = setup_test_context(&mut file, "1.0.0");
172-
173-
let version = Version::new(&context);
174-
175-
assert_eq!(version.version.to_string(), "1.0.0");
176-
assert_eq!(version.line, "version = \"1.0.0\"");
177-
assert_eq!(
178-
version.config_path,
179-
file.path().to_str().unwrap().to_string()
180-
);
181-
}
182-
183-
#[test]
184-
fn test_set_version() {
185-
let mut file = NamedTempFile::new().unwrap();
186-
let context = setup_test_context(&mut file, "1.0.0");
187-
188-
let mut version = Version::new(&context);
189-
assert_eq!(version.version.to_string(), "1.0.0");
190-
191-
version.set(semver::Version::parse("2.0.0").unwrap());
192-
assert_eq!(version.version.to_string(), "2.0.0");
193-
}
194-
195-
#[test]
196-
fn test_bump_version() {
197-
let mut file = NamedTempFile::new().unwrap();
198-
let context = setup_test_context(&mut file, "1.0.0");
199-
200-
let mut version = Version::new(&context);
201-
assert_eq!(version.version.to_string(), "1.0.0");
202-
203-
version.bump(Bump::Major, None);
204-
assert_eq!(version.version.to_string(), "2.0.0");
205-
206-
version.bump(Bump::Minor, None);
207-
assert_eq!(version.version.to_string(), "2.1.0");
208-
209-
version.bump(Bump::Patch, None);
210-
assert_eq!(version.version.to_string(), "2.1.1");
211-
212-
version.bump(Bump::Pre("alpha".to_string()), None);
213-
assert_eq!(version.version.to_string(), "2.1.1-alpha.1");
214-
215-
version.bump(Bump::Pre("alpha".to_string()), None);
216-
assert_eq!(version.version.to_string(), "2.1.1-alpha.2");
217-
218-
version.bump(Bump::Pre("beta".to_string()), None);
219-
assert_eq!(version.version.to_string(), "2.1.1-beta.1");
220-
221-
version.bump(Bump::Major, Some("beta".to_string()));
222-
assert_eq!(version.version.to_string(), "3.0.0-beta.1");
223-
224-
version.bump(Bump::Major, None);
225-
assert_eq!(version.version.to_string(), "4.0.0");
226-
}
227-
}

0 commit comments

Comments
 (0)