-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fn_args_layout = "Block" | ||
array_layout = "Block" | ||
where_style = "Rfc" | ||
generics_indent = "Block" | ||
fn_call_style = "Block" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,7 @@ impl CodeBlock { | |
let path = Path::new(root.as_ref()).join(&self.filename()); | ||
let parent = match path.parent() { | ||
Some(p) => p, | ||
None => bail!("Can't create file for code block, path has no parent directory"), | ||
None => bail!("Can't create file for code block, path has no parent directory",), | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
}; | ||
|
||
create_dir_all(parent)?; | ||
|
@@ -82,7 +82,8 @@ mod test { | |
|
||
#[test] | ||
fn parsing() { | ||
let example = unindent(r#" | ||
let example = unindent( | ||
r#" | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
# Lorem ipsum | ||
## Shell | ||
|
@@ -98,7 +99,8 @@ mod test { | |
println!("Dolor sit amet"); | ||
} | ||
``` | ||
"#); | ||
"#, | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
); | ||
|
||
let markdown = ::pulldown_cmark::Parser::new(&example); | ||
let code_blocks = ::extract_code_blocks(markdown).unwrap(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,22 @@ pub fn app() -> App<'static, 'static> { | |
.version(crate_version!()) | ||
.author("Pascal Hertleif <[email protected]>") | ||
.about("Extract code blocks from Markdown and save them as files.") | ||
.arg(Arg::with_name("input_file") | ||
.help("The input markdown file") | ||
.index(1) | ||
.required(true) | ||
.arg( | ||
Arg::with_name("input_file") | ||
.help("The input markdown file") | ||
.index(1) | ||
.required(true), | ||
This comment has been minimized.
Sorry, something went wrong. |
||
) | ||
.arg(Arg::with_name("target_dir") | ||
.help("The target directory") | ||
.short("o") | ||
.takes_value(true) | ||
.arg( | ||
Arg::with_name("target_dir") | ||
.help("The target directory") | ||
.short("o") | ||
.takes_value(true), | ||
) | ||
.arg(Arg::with_name("v") | ||
.short("v") | ||
.help("Enable logging, use multiple `v`s to increase verbosity") | ||
.multiple(true) | ||
.arg( | ||
Arg::with_name("v") | ||
.short("v") | ||
.help("Enable logging, use multiple `v`s to increase verbosity") | ||
.multiple(true), | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,8 @@ fn try_main() -> Result<()> { | |
// Parse markdown file | ||
let input = { | ||
let mut res = String::new(); | ||
let mut f = File::open(input_file) | ||
.chain_err(|| format!("Error opening file `{}`", input_file))?; | ||
let mut f = | ||
File::open(input_file).chain_err(|| format!("Error opening file `{}`", input_file))?; | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
f.read_to_string(&mut res) | ||
.chain_err(|| format!("Error reading file `{}`", input_file))?; | ||
info!("Read file `{}`", input_file); | ||
|
@@ -48,7 +48,10 @@ fn try_main() -> Result<()> { | |
|
||
let code_blocks = waltz::extract_code_blocks(parser)?; | ||
|
||
info!("Found {} code blocks (not all might have file names)", code_blocks.len()); | ||
info!( | ||
"Found {} code blocks (not all might have file names)", | ||
code_blocks.len(), | ||
); | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
|
||
// Output files | ||
for code_block in code_blocks.iter().filter(|cb| cb.has_filename()) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ use utils::*; | |
|
||
#[test] | ||
fn concat() { | ||
given(r#" | ||
given( | ||
r#" | ||
# Getting started | ||
First of all, create a simple `Cargo.toml` file: | ||
|
@@ -45,15 +46,22 @@ fn concat() { | |
} | ||
} | ||
``` | ||
"#) | ||
.running(waltz) | ||
.creates(file("Cargo.toml").containing(r#" | ||
"#, | ||
) | ||
.running(waltz) | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
.creates( | ||
file("Cargo.toml").containing( | ||
r#" | ||
[package] | ||
authors = ["Pascal Hertleif <[email protected]>"] | ||
name = "foo" | ||
version = "0.1.0" | ||
"#)) | ||
.creates(file("src/lib.rs").containing(r#" | ||
"#, | ||
), | ||
) | ||
.creates( | ||
file("src/lib.rs").containing( | ||
r#" | ||
struct Foo { | ||
x: i32, | ||
} | ||
|
@@ -69,6 +77,8 @@ fn concat() { | |
Foo { x: 42 } | ||
} | ||
} | ||
"#)) | ||
.running(cargo_check); | ||
"#, | ||
), | ||
) | ||
.running(cargo_check); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ use utils::*; | |
|
||
#[test] | ||
fn simple() { | ||
given(r#" | ||
given( | ||
r#" | ||
# Getting started | ||
First of all, create a simple `Cargo.toml` file: | ||
|
@@ -22,25 +23,35 @@ fn simple() { | |
println!("Hello, world!"); | ||
} | ||
``` | ||
"#) | ||
.running(waltz) | ||
.creates(file("Cargo.toml").containing(r#" | ||
"#, | ||
) | ||
.running(waltz) | ||
.creates( | ||
file("Cargo.toml").containing( | ||
r#" | ||
[package] | ||
authors = ["Pascal Hertleif <[email protected]>"] | ||
name = "foo" | ||
version = "0.1.0" | ||
"#)) | ||
.creates(file("src/main.rs").containing(r#" | ||
"#, | ||
), | ||
) | ||
.creates( | ||
file("src/main.rs").containing( | ||
r#" | ||
fn main() { | ||
println!("Hello, world!"); | ||
} | ||
"#)) | ||
.running(|cwd| main(cwd).prints("Hello, world!")); | ||
"#, | ||
), | ||
) | ||
.running(|cwd| main(cwd).prints("Hello, world!")); | ||
} | ||
|
||
#[test] | ||
fn complex_paths() { | ||
given(r#" | ||
given( | ||
r#" | ||
First off: | ||
```toml,file=Cargo.toml | ||
|
@@ -61,11 +72,10 @@ fn complex_paths() { | |
println!("Sup dawg I herd u likd nested dirs"); | ||
} | ||
``` | ||
"#) | ||
.running(waltz) | ||
.creates(file("Cargo.toml")) | ||
.creates(file("src/bin/lolwut/main.rs")) | ||
.running(|cwd| binary(cwd, "lolwut") | ||
.prints("Sup dawg") | ||
); | ||
"#, | ||
) | ||
.running(waltz) | ||
.creates(file("Cargo.toml")) | ||
.creates(file("src/bin/lolwut/main.rs")) | ||
.running(|cwd| binary(cwd, "lolwut").prints("Sup dawg")); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,34 +25,29 @@ pub fn file(path: &str) -> FileAssert { | |
|
||
pub fn waltz(cwd: &Path) -> CliAssert { | ||
CliAssert::main_binary() | ||
.with_args(&[ | ||
"-vvv", | ||
cwd.join("test.md").to_str().unwrap(), | ||
"-o", cwd.to_str().unwrap(), | ||
]) | ||
.with_args(&["-vvv", cwd.join("test.md").to_str().unwrap(), "-o", cwd.to_str().unwrap()],) | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
.succeeds() | ||
} | ||
|
||
pub fn main(cwd: &Path) -> CliAssert { | ||
CliAssert::command(&[ | ||
"cargo", "run", | ||
"--manifest-path", cwd.join("Cargo.toml").to_str().unwrap(), | ||
]) | ||
CliAssert::command(&["cargo", "run", "--manifest-path", cwd.join("Cargo.toml").to_str().unwrap()],) | ||
} | ||
|
||
pub fn binary(cwd: &Path, name: &str) -> CliAssert { | ||
CliAssert::command(&[ | ||
"cargo", "run", | ||
"--manifest-path", cwd.join("Cargo.toml").to_str().unwrap(), | ||
"--bin", name, | ||
]) | ||
CliAssert::command( | ||
&[ | ||
"cargo", | ||
"run", | ||
"--manifest-path", | ||
cwd.join("Cargo.toml").to_str().unwrap(), | ||
"--bin", | ||
name, | ||
], | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
) | ||
} | ||
|
||
fn cargo(cwd: &Path, subcommand: &str) -> CliAssert { | ||
CliAssert::command(&[ | ||
"cargo", subcommand, | ||
"--manifest-path", cwd.join("Cargo.toml").to_str().unwrap(), | ||
]) | ||
CliAssert::command(&["cargo", subcommand, "--manifest-path", cwd.join("Cargo.toml").to_str().unwrap()],) | ||
} | ||
|
||
pub fn cargo_check(cwd: &Path) -> CliAssert { | ||
|
@@ -85,27 +80,25 @@ impl Assert { | |
fn with_file(content: &str) -> Self { | ||
let a = Assert::default(); | ||
|
||
create_dir_all(&a.output_dir) | ||
.expect("error creating output dir"); | ||
create_dir_all(&a.output_dir).expect("error creating output dir"); | ||
|
||
let mut f = File::create(a.output_dir.join("test.md")) | ||
.expect("error create md file"); | ||
let mut f = File::create(a.output_dir.join("test.md")).expect("error create md file"); | ||
f.write_all(unindent(content).as_bytes()) | ||
.expect("error writing md file"); | ||
|
||
a | ||
} | ||
|
||
pub fn running<F>(&self, cmd: F) -> &Self where | ||
pub fn running<F>(&self, cmd: F) -> &Self | ||
where | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
F: for<'cwd> Fn(&'cwd Path) -> CliAssert, | ||
{ | ||
cmd(&self.output_dir).unwrap(); | ||
self | ||
} | ||
|
||
pub fn creates(&self, fa: FileAssert) -> &Self { | ||
fa.context(self.output_dir.to_owned()) | ||
.unwrap(); | ||
fa.context(self.output_dir.to_owned()).unwrap(); | ||
self | ||
} | ||
} | ||
|
@@ -137,14 +130,16 @@ impl FileAssert { | |
} | ||
|
||
fn unwrap(self) { | ||
let dir = self.working_dir.expect(&format!("No working dir set for `{}`", self.path)); | ||
let dir = self.working_dir | ||
.expect(&format!("No working dir set for `{}`", self.path)); | ||
let path = PathBuf::from(dir).join(&self.path); | ||
|
||
let mut f = File::open(&path).expect(&format!("no file at {:?}", path)); | ||
|
||
if let Some(expected_content) = self.content { | ||
let mut content = String::new(); | ||
f.read_to_string(&mut content).expect(&format!("failed to read {:?}", path)); | ||
f.read_to_string(&mut content) | ||
.expect(&format!("failed to read {:?}", path)); | ||
This comment has been minimized.
Sorry, something went wrong.
killercup
Author
Owner
|
||
|
||
let diff = Changeset::new(&content, &unindent(&expected_content), "\n"); | ||
if diff.distance > 0 { | ||
|
That seems just wrong