Skip to content

Commit

Permalink
added codecov task
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Jul 14, 2017
1 parent ecd12a8 commit c5fba6c
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 46 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ script:
- sudo make install
- cd ../..
- rm -rf kcov-$KCOV_VERSION
- cargo make coverage-flow
- bash <(curl -s https://codecov.io/bash)
- echo "Uploaded Code Coverage"
- cargo run -- make codecov-flow
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-make"
version = "0.3.20"
version = "0.3.21"
authors = ["Sagie Gur-Ari <sagiegurari@gmail.com>"]
description = "Rust task runner and build tool."
license = "Apache-2.0"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ The following are some of the main flows that can be used without any need of an
* **publish-flow** - Cleans old target directory and publishes the project.
* **build-flow** - Runs full cycle of build, tests, security checks, dependencies up to date validations and documentation generation.<br>This flow can be used to make sure your project is fully tested and up to date.
* **coverage-flow** - Creates coverage report from all unit and integration tests (not supported on windows).
* **codecov-flow** - Runs the coverage-flow and uploads the coverage results to codecov (not supported on windows).

<a name="usage-init-end-tasks"></a>
### Init and End tasks
Expand Down Expand Up @@ -701,6 +702,7 @@ See [contributing guide](.github/CONTRIBUTING.md)

| Date | Version | Description |
| ----------- | ------- | ----------- |
| 2017-07-14 | v0.3.21 | Added codecov task in default toml |
| 2017-07-14 | v0.3.20 | Added coverage task in default toml |
| 2017-07-14 | v0.3.16 | Added more environment variables based on target environment and rust compiler |
| 2017-07-13 | v0.3.15 | Added common init and end tasks |
Expand Down
2 changes: 2 additions & 0 deletions docs/_includes/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ The following are some of the main flows that can be used without any need of an
* **publish-flow** - Cleans old target directory and publishes the project.
* **build-flow** - Runs full cycle of build, tests, security checks, dependencies up to date validations and documentation generation.<br>This flow can be used to make sure your project is fully tested and up to date.
* **coverage-flow** - Creates coverage report from all unit and integration tests (not supported on windows).
* **codecov-flow** - Runs the coverage-flow and uploads the coverage results to codecov (not supported on windows).

<a name="usage-init-end-tasks"></a>
### Init and End tasks
Expand Down Expand Up @@ -673,6 +674,7 @@ See [contributing guide](https://github.com/sagiegurari/cargo-make/blob/master/.

| Date | Version | Description |
| ----------- | ------- | ----------- |
| 2017-07-14 | v0.3.21 | Added codecov task in default toml |
| 2017-07-14 | v0.3.20 | Added coverage task in default toml |
| 2017-07-14 | v0.3.16 | Added more environment variables based on target environment and rust compiler |
| 2017-07-13 | v0.3.15 | Added common init and end tasks |
Expand Down
92 changes: 71 additions & 21 deletions docs/api/src/cargo_make/command.rs.html
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,31 @@
<span id="175">175</span>
<span id="176">176</span>
<span id="177">177</span>
<span id="178">178</span>
<span id="179">179</span>
<span id="180">180</span>
<span id="181">181</span>
<span id="182">182</span>
<span id="183">183</span>
<span id="184">184</span>
<span id="185">185</span>
<span id="186">186</span>
<span id="187">187</span>
<span id="188">188</span>
<span id="189">189</span>
<span id="190">190</span>
<span id="191">191</span>
<span id="192">192</span>
<span id="193">193</span>
<span id="194">194</span>
<span id="195">195</span>
<span id="196">196</span>
<span id="197">197</span>
<span id="198">198</span>
<span id="199">199</span>
<span id="200">200</span>
<span id="201">201</span>
<span id="202">202</span>
</pre><pre class="rust ">
<span class="doccomment">//! # command</span>
<span class="doccomment">//!</span>
Expand Down Expand Up @@ -262,6 +287,51 @@
}
}

<span class="kw">fn</span> <span class="ident">create_script</span>(
<span class="ident">logger</span>: <span class="kw-2">&amp;</span><span class="ident">Logger</span>,
<span class="ident">script_lines</span>: <span class="kw-2">&amp;</span><span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">String</span><span class="op">&gt;</span>,
) <span class="op">-&gt;</span> <span class="ident">String</span> {
<span class="kw">let</span> <span class="ident">cwd_holder</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">current_dir</span>() {
<span class="prelude-val">Ok</span>(<span class="ident">value</span>) <span class="op">=&gt;</span> <span class="ident">value</span>,
<span class="prelude-val">Err</span>(<span class="ident">error</span>) <span class="op">=&gt;</span> {
<span class="ident">logger</span>.<span class="ident">error</span>(<span class="string">&quot;Unable to get current working directory.&quot;</span>, <span class="kw-2">&amp;</span>[], <span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span><span class="ident">error</span>));
<span class="macro">panic</span><span class="macro">!</span>(<span class="string">&quot;Unable to get current working directory, error: {}&quot;</span>, <span class="ident">error</span>);
}
};

<span class="kw">let</span> <span class="ident">cwd</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">cwd_holder</span>.<span class="ident">to_str</span>() {
<span class="prelude-val">Some</span>(<span class="ident">cwd_str</span>) <span class="op">=&gt;</span> <span class="ident">cwd_str</span>.<span class="ident">clone</span>(),
<span class="prelude-val">None</span> <span class="op">=&gt;</span> {
<span class="ident">logger</span>.<span class="ident">error</span>::<span class="op">&lt;</span>()<span class="op">&gt;</span>(<span class="string">&quot;Unable to get current working directory.&quot;</span>, <span class="kw-2">&amp;</span>[], <span class="prelude-val">None</span>);
<span class="macro">panic</span><span class="macro">!</span>(<span class="string">&quot;Unable to get current working directory&quot;</span>);
}
};

<span class="comment">// get local copy</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">mut_script_lines</span> <span class="op">=</span> <span class="ident">script_lines</span>.<span class="ident">clone</span>();

<span class="comment">// create cd command</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">cd_command</span> <span class="op">=</span> <span class="string">&quot;cd &quot;</span>.<span class="ident">to_string</span>();
<span class="ident">cd_command</span>.<span class="ident">push_str</span>(<span class="ident">cwd</span>);

<span class="comment">// check if first line is shebang line</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">insert_index</span> <span class="op">=</span> <span class="kw">if</span> <span class="ident">mut_script_lines</span>[<span class="number">0</span>].<span class="ident">starts_with</span>(<span class="string">&quot;#!&quot;</span>) {
<span class="number">1</span>
} <span class="kw">else</span> {
<span class="number">0</span>
};

<span class="kw">if</span> <span class="op">!</span><span class="macro">cfg</span><span class="macro">!</span>(<span class="ident">windows</span>) {
<span class="ident">mut_script_lines</span>.<span class="ident">insert</span>(<span class="ident">insert_index</span>, <span class="string">&quot;set -e&quot;</span>.<span class="ident">to_string</span>());
<span class="ident">insert_index</span> <span class="op">=</span> <span class="ident">insert_index</span> <span class="op">+</span> <span class="number">1</span>;
}
<span class="ident">mut_script_lines</span>.<span class="ident">insert</span>(<span class="ident">insert_index</span>, <span class="ident">cd_command</span>);

<span class="ident">mut_script_lines</span>.<span class="ident">push</span>(<span class="string">&quot;\n&quot;</span>.<span class="ident">to_string</span>());

<span class="ident">mut_script_lines</span>.<span class="ident">join</span>(<span class="string">&quot;\n&quot;</span>)
}

<span class="doccomment">/// Runs the requested script text and panics in case of any script error.</span>
<span class="kw">pub</span> <span class="kw">fn</span> <span class="ident">run_script</span>(
<span class="ident">logger</span>: <span class="kw-2">&amp;</span><span class="ident">Logger</span>,
Expand Down Expand Up @@ -299,27 +369,7 @@
<span class="prelude-val">Ok</span>(<span class="ident">file</span>) <span class="op">=&gt;</span> <span class="ident">file</span>,
};

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">text</span> <span class="op">=</span> <span class="ident">script_lines</span>.<span class="ident">join</span>(<span class="string">&quot;\n&quot;</span>);

<span class="kw">let</span> <span class="ident">cwd_holder</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">current_dir</span>() {
<span class="prelude-val">Ok</span>(<span class="ident">value</span>) <span class="op">=&gt;</span> <span class="ident">value</span>,
<span class="prelude-val">Err</span>(<span class="ident">error</span>) <span class="op">=&gt;</span> {
<span class="ident">logger</span>.<span class="ident">error</span>(<span class="string">&quot;Unable to get current working directory.&quot;</span>, <span class="kw-2">&amp;</span>[], <span class="prelude-val">Some</span>(<span class="kw-2">&amp;</span><span class="ident">error</span>));
<span class="macro">panic</span><span class="macro">!</span>(<span class="string">&quot;Unable to get current working directory, error: {}&quot;</span>, <span class="ident">error</span>);
}
};

<span class="kw">let</span> <span class="ident">cwd</span> <span class="op">=</span> <span class="kw">match</span> <span class="ident">cwd_holder</span>.<span class="ident">to_str</span>() {
<span class="prelude-val">Some</span>(<span class="ident">cwd_str</span>) <span class="op">=&gt;</span> <span class="ident">cwd_str</span>.<span class="ident">clone</span>(),
<span class="prelude-val">None</span> <span class="op">=&gt;</span> {
<span class="ident">logger</span>.<span class="ident">error</span>::<span class="op">&lt;</span>()<span class="op">&gt;</span>(<span class="string">&quot;Unable to get current working directory.&quot;</span>, <span class="kw-2">&amp;</span>[], <span class="prelude-val">None</span>);
<span class="macro">panic</span><span class="macro">!</span>(<span class="string">&quot;Unable to get current working directory&quot;</span>);
}
};

<span class="ident">text</span>.<span class="ident">insert_str</span>(<span class="number">0</span>, <span class="string">&quot;\n&quot;</span>);
<span class="ident">text</span>.<span class="ident">insert_str</span>(<span class="number">0</span>, <span class="ident">cwd</span>);
<span class="ident">text</span>.<span class="ident">insert_str</span>(<span class="number">0</span>, <span class="string">&quot;cd &quot;</span>);
<span class="kw">let</span> <span class="ident">text</span> <span class="op">=</span> <span class="ident">create_script</span>(<span class="kw-2">&amp;</span><span class="ident">logger</span>, <span class="kw-2">&amp;</span><span class="ident">script_lines</span>);

<span class="kw">match</span> <span class="ident">file</span>.<span class="ident">write_all</span>(<span class="ident">text</span>.<span class="ident">as_bytes</span>()) {
<span class="prelude-val">Err</span>(<span class="ident">error</span>) <span class="op">=&gt;</span> {
Expand Down
67 changes: 46 additions & 21 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,51 @@ pub fn validate_exit_code(
}
}

fn create_script(
logger: &Logger,
script_lines: &Vec<String>,
) -> String {
let cwd_holder = match current_dir() {
Ok(value) => value,
Err(error) => {
logger.error("Unable to get current working directory.", &[], Some(&error));
panic!("Unable to get current working directory, error: {}", error);
}
};

let cwd = match cwd_holder.to_str() {
Some(cwd_str) => cwd_str.clone(),
None => {
logger.error::<()>("Unable to get current working directory.", &[], None);
panic!("Unable to get current working directory");
}
};

// get local copy
let mut mut_script_lines = script_lines.clone();

// create cd command
let mut cd_command = "cd ".to_string();
cd_command.push_str(cwd);

// check if first line is shebang line
let mut insert_index = if mut_script_lines[0].starts_with("#!") {
1
} else {
0
};

if !cfg!(windows) {
mut_script_lines.insert(insert_index, "set -e".to_string());
insert_index = insert_index + 1;
}
mut_script_lines.insert(insert_index, cd_command);

mut_script_lines.push("\n".to_string());

mut_script_lines.join("\n")
}

/// Runs the requested script text and panics in case of any script error.
pub fn run_script(
logger: &Logger,
Expand Down Expand Up @@ -76,27 +121,7 @@ pub fn run_script(
Ok(file) => file,
};

let mut text = script_lines.join("\n");

let cwd_holder = match current_dir() {
Ok(value) => value,
Err(error) => {
logger.error("Unable to get current working directory.", &[], Some(&error));
panic!("Unable to get current working directory, error: {}", error);
}
};

let cwd = match cwd_holder.to_str() {
Some(cwd_str) => cwd_str.clone(),
None => {
logger.error::<()>("Unable to get current working directory.", &[], None);
panic!("Unable to get current working directory");
}
};

text.insert_str(0, "\n");
text.insert_str(0, cwd);
text.insert_str(0, "cd ");
let text = create_script(&logger, &script_lines);

match file.write_all(text.as_bytes()) {
Err(error) => {
Expand Down
39 changes: 39 additions & 0 deletions src/command_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
use super::*;
use log;
use std::env::current_dir;
use std::io::ErrorKind;
use types::Task;

#[test]
fn create_script_no_shebang() {
let logger = log::create("error");

let script_lines = vec!["echo test".to_string()];
let cwd = current_dir().unwrap();
let mut expected_script = "".to_string();
if !cfg!(windows) {
expected_script.push_str("set -e\n");
}
expected_script.push_str("cd ");
expected_script.push_str(cwd.to_str().unwrap());
expected_script.push_str("\necho test\n\n");

let script = create_script(&logger, &script_lines);

assert_eq!(script, expected_script);
}

#[test]
fn create_script_with_shebang() {
let logger = log::create("error");

let script_lines = vec!["#!/bin/bash".to_string(), "echo test".to_string()];
let cwd = current_dir().unwrap();
let mut expected_script = "#!/bin/bash\n".to_string();
if !cfg!(windows) {
expected_script.push_str("set -e\n");
}
expected_script.push_str("cd ");
expected_script.push_str(cwd.to_str().unwrap());
expected_script.push_str("\necho test\n\n");

let script = create_script(&logger, &script_lines);

assert_eq!(script, expected_script);
}

#[test]
#[should_panic]
fn validate_exit_code_error() {
Expand Down
9 changes: 9 additions & 0 deletions src/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,12 @@ dependencies = [
"coverage",
"post-coverage"
]

[tasks.codecov-flow]
dependencies = [
"coverage-flow"
]
script = [
"#!/bin/bash",
"bash <(curl -s https://codecov.io/bash)"
]

0 comments on commit c5fba6c

Please sign in to comment.