Use custom tarpaulin Github action with taskwarrior

This commit is contained in:
2022-01-10 21:43:03 +01:00
parent 4751ea5ff9
commit f5758f8aa9
6 changed files with 92 additions and 33 deletions

View File

@@ -0,0 +1,22 @@
name: 'cargo tarpaulin'
description: 'Gather Rust code coverage information with Tarpaulin'
inputs:
version:
description: 'The version of cargo-tarpaulin to install'
required: true
default: '0.19.0'
args:
required: false
description: 'Extra command line arguments passed to cargo-tarpaulin'
out-type:
description: 'Output format of coverage report [possible values: Json, Toml, Stdout, Xml, Html, Lcov]'
required: false
default: 'Xml'
runs:
using: 'composite'
steps:
- run: ${{ github.action_path }}/cargo-tarpaulin.sh ${{ inputs.out-type }} ${{ inputs.version }} ${{ inputs.args }}
shell: bash

View File

@@ -0,0 +1,15 @@
#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y --no-install-recommends openssl taskwarrior
out_type="$1"
version="$2"
args="$3"
tar_file="cargo-tarpaulin-${version}-travis.tar.gz"
wget "https://github.com/xd009642/tarpaulin/releases/download/${version}/${tar_file}"
tar zxvf "$tar_file"
chmod +x cargo-tarpaulin
exec env RUST_LOG=debug ./cargo-tarpaulin tarpaulin --ignore-tests -o "$out_type" $args

View File

@@ -20,7 +20,7 @@ jobs:
options: --manifest-path=Cargo.toml options: --manifest-path=Cargo.toml
build_and_test: build_and_test:
name: contextswitch-api name: Build & test
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
@@ -41,9 +41,8 @@ jobs:
args: -- -D warnings args: -- -D warnings
- name: Run cargo-tarpaulin - name: Run cargo-tarpaulin
uses: actions-rs/tarpaulin@v0.1 uses: ./.github/actions/cargo-tarpaulin-action/
with: with:
version: '0.15.0'
args: '-- --test-threads 1' args: '-- --test-threads 1'
- name: Upload to codecov.io - name: Upload to codecov.io

View File

@@ -12,12 +12,16 @@ pub mod taskwarrior;
#[derive(Deserialize)] #[derive(Deserialize)]
struct TaskQuery { struct TaskQuery {
filter: String, filter: Option<String>,
} }
#[tracing::instrument(level = "debug", skip(task_query))] #[tracing::instrument(level = "debug", skip(task_query))]
async fn list_tasks(task_query: web::Query<TaskQuery>) -> Result<HttpResponse, Error> { async fn list_tasks(task_query: web::Query<TaskQuery>) -> Result<HttpResponse, Error> {
let tasks = contextswitch::export(task_query.filter.split(' ').collect())?; let filter = task_query
.filter
.as_ref()
.map_or(vec![], |filter| filter.split(' ').collect());
let tasks = contextswitch::export(filter)?;
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("application/json") .content_type("application/json")

View File

@@ -7,6 +7,7 @@ use std::io::Error;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::str; use std::str;
use tracing::debug;
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, PartialEq)] #[derive(Debug, Serialize, Deserialize, PartialEq)]
@@ -44,29 +45,9 @@ pub struct Task {
pub contextswitch: Option<String>, pub contextswitch: Option<String>,
} }
pub fn load_config(task_data_location: Option<&str>) -> String { fn write_default_config(data_location: &str) -> String {
if let Ok(taskrc_location) = env::var("TASKRC") {
let mut taskrc = Ini::new();
taskrc
.load(&taskrc_location)
.unwrap_or_else(|_| panic!("Cannot load taskrc file {}", taskrc_location));
return taskrc.get("default", "data.location").unwrap_or_else(|| {
panic!(
"'data.location' must be set in taskrc file {}",
taskrc_location
)
});
}
let data_location = task_data_location
.map(|s| s.to_string())
.unwrap_or_else(|| {
env::var("TASK_DATA_LOCATION")
.expect("Expecting TASKRC or TASK_DATA_LOCATION environment variable value")
});
let mut taskrc = Ini::new(); let mut taskrc = Ini::new();
taskrc.setstr("default", "data.location", Some(&data_location)); taskrc.setstr("default", "data.location", Some(data_location));
taskrc.setstr("default", "uda.contextswitch.type", Some("string")); taskrc.setstr("default", "uda.contextswitch.type", Some("string"));
taskrc.setstr( taskrc.setstr(
"default", "default",
@@ -79,25 +60,63 @@ pub fn load_config(task_data_location: Option<&str>) -> String {
let taskrc_location = taskrc_path.to_str().unwrap(); let taskrc_location = taskrc_path.to_str().unwrap();
taskrc.write(taskrc_location).unwrap(); taskrc.write(taskrc_location).unwrap();
env::set_var("TASKRC", taskrc_location); taskrc_location.into()
data_location
} }
pub fn load_config(task_data_location: Option<&str>) -> String {
if let Ok(taskrc_location) = env::var("TASKRC") {
let mut taskrc = Ini::new();
taskrc
.load(&taskrc_location)
.unwrap_or_else(|_| panic!("Cannot load taskrc file {}", taskrc_location));
let data_location = taskrc.get("default", "data.location").unwrap_or_else(|| {
panic!(
"'data.location' must be set in taskrc file {}",
taskrc_location
)
});
debug!(
"Extracted data location `{}` from existing taskrc `{}`",
data_location, taskrc_location
);
data_location
} else {
let data_location = task_data_location
.map(|s| s.to_string())
.unwrap_or_else(|| {
env::var("TASK_DATA_LOCATION")
.expect("Expecting TASKRC or TASK_DATA_LOCATION environment variable value")
});
let taskrc_location = write_default_config(&data_location);
env::set_var("TASKRC", &taskrc_location);
debug!("Default taskrc written in `{}`", &taskrc_location);
data_location
}
}
#[tracing::instrument(level = "debug")]
pub fn export(filters: Vec<&str>) -> Result<Vec<Task>, Error> { pub fn export(filters: Vec<&str>) -> Result<Vec<Task>, Error> {
let mut args = vec!["export"]; let mut args = vec!["export"];
args.extend(filters); args.extend(filters);
let export_output = Command::new("task").args(args).output()?; let export_output = Command::new("task").args(args).output()?;
let output = String::from_utf8(export_output.stdout.clone()).unwrap();
debug!("export output: {}", output);
let tasks: Vec<Task> = serde_json::from_slice(&export_output.stdout)?; let tasks: Vec<Task> = serde_json::from_slice(&export_output.stdout)?;
Ok(tasks) Ok(tasks)
} }
#[tracing::instrument(level = "debug")]
pub fn add(add_args: Vec<&str>) -> Result<(), Error> { pub fn add(add_args: Vec<&str>) -> Result<(), Error> {
let mut args = vec!["add"]; let mut args = vec!["add"];
args.extend(add_args); args.extend(add_args);
Command::new("task").args(args).output()?; let add_output = Command::new("task").args(args).output()?;
let output = String::from_utf8(add_output.stdout).unwrap();
debug!("add output: {}", output);
Ok(()) Ok(())
} }

View File

@@ -5,13 +5,13 @@ use contextswitch_types::Task;
#[tokio::test] #[tokio::test]
async fn list_tasks() { async fn list_tasks() {
let task_data_path = test_helper::setup_tasks();
let address = test_helper::spawn_app(); let address = test_helper::spawn_app();
let task_data_path = test_helper::setup_tasks();
let client = reqwest::Client::new(); let client = reqwest::Client::new();
taskwarrior::add(vec!["test1", "contextswitch:'{\"test\": 1}'"]).unwrap(); taskwarrior::add(vec!["test1", "contextswitch:'{\"test\": 1}'"]).unwrap();
let response: reqwest::Response = client let response: reqwest::Response = client
.get(&format!("{}/tasks?filter=ls", &address)) .get(&format!("{}/tasks", &address))
.send() .send()
.await .await
.expect("Failed to execute request."); .expect("Failed to execute request.");