Compare commits
5 Commits
5c401fe18f
...
46b29c28e4
| Author | SHA1 | Date | |
|---|---|---|---|
|
46b29c28e4
|
|||
|
b359b5123e
|
|||
|
3f3f887cfe
|
|||
|
8207ab958d
|
|||
|
884d4bee1f
|
22
.github/actions/cargo-tarpaulin-action/action.yml
vendored
Normal file
22
.github/actions/cargo-tarpaulin-action/action.yml
vendored
Normal 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
|
||||||
15
.github/actions/cargo-tarpaulin-action/cargo-tarpaulin.sh
vendored
Executable file
15
.github/actions/cargo-tarpaulin-action/cargo-tarpaulin.sh
vendored
Executable 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
|
||||||
16
.github/workflows/audit.yml
vendored
Normal file
16
.github/workflows/audit.yml
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
name: Security audit
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
paths:
|
||||||
|
- '**/Cargo.toml'
|
||||||
|
- '**/Cargo.lock'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
security_audit:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
- uses: actions-rs/audit-check@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
57
.github/workflows/ci.yml
vendored
Normal file
57
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
on: [push]
|
||||||
|
|
||||||
|
name: CI
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
format:
|
||||||
|
name: rustfmt
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: nightly
|
||||||
|
components: rustfmt
|
||||||
|
override: true
|
||||||
|
- uses: LoliGothick/rustfmt-check@v0.2
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
flags: --all
|
||||||
|
options: --manifest-path=Cargo.toml
|
||||||
|
|
||||||
|
build_and_test:
|
||||||
|
name: Build & test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
components: clippy
|
||||||
|
|
||||||
|
- uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: build
|
||||||
|
args: --release --all-features
|
||||||
|
|
||||||
|
- uses: actions-rs/clippy-check@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
args: -- -D warnings
|
||||||
|
|
||||||
|
- name: Run cargo-tarpaulin
|
||||||
|
uses: ./.github/actions/cargo-tarpaulin-action/
|
||||||
|
with:
|
||||||
|
args: '-- --test-threads 1'
|
||||||
|
|
||||||
|
- name: Upload to codecov.io
|
||||||
|
uses: codecov/codecov-action@v1.0.2
|
||||||
|
with:
|
||||||
|
token: ${{secrets.CODECOV_TOKEN}}
|
||||||
|
|
||||||
|
- name: Archive code coverage results
|
||||||
|
uses: actions/upload-artifact@v1
|
||||||
|
with:
|
||||||
|
name: code-coverage-report
|
||||||
|
path: cobertura.xml
|
||||||
31
Cargo.lock
generated
31
Cargo.lock
generated
@@ -329,16 +329,17 @@ dependencies = [
|
|||||||
"configparser",
|
"configparser",
|
||||||
"contextswitch-types",
|
"contextswitch-types",
|
||||||
"dotenv",
|
"dotenv",
|
||||||
|
"lazy_static",
|
||||||
"listenfd",
|
"listenfd",
|
||||||
"mktemp",
|
"mktemp",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-actix-web",
|
"tracing-actix-web",
|
||||||
"tracing-bunyan-formatter",
|
|
||||||
"tracing-log",
|
"tracing-log",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"uuid",
|
"uuid",
|
||||||
@@ -347,6 +348,7 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "contextswitch-types"
|
name = "contextswitch-types"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
source = "git+https://github.com/dax/contextswitch-types.git#79b61a96b5ba708769715d603bb1cbe3c14a0045"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -560,16 +562,6 @@ dependencies = [
|
|||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "gethostname"
|
|
||||||
version = "0.2.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e692e296bfac1d2533ef168d0b60ff5897b8b70a4009276834014dd8924cc028"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.3"
|
version = "0.2.3"
|
||||||
@@ -1538,23 +1530,6 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tracing-bunyan-formatter"
|
|
||||||
version = "0.3.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1cb2ad6aa9b1c637d84c54db002275bbf72a7f3c6fed80f8b33f5af0c39027e9"
|
|
||||||
dependencies = [
|
|
||||||
"gethostname",
|
|
||||||
"log",
|
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
"time 0.3.5",
|
|
||||||
"tracing",
|
|
||||||
"tracing-core",
|
|
||||||
"tracing-log",
|
|
||||||
"tracing-subscriber",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tracing-core"
|
name = "tracing-core"
|
||||||
version = "0.1.21"
|
version = "0.1.21"
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ path = "src/main.rs"
|
|||||||
name = "contextswitch-api"
|
name = "contextswitch-api"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
contextswitch-types = { path = "../types" }
|
contextswitch-types = { git = "https://github.com/dax/contextswitch-types.git" }
|
||||||
actix-web = "=4.0.0-beta.19"
|
actix-web = "=4.0.0-beta.19"
|
||||||
actix-http = "=3.0.0-beta.18"
|
actix-http = "=3.0.0-beta.18"
|
||||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
||||||
@@ -26,10 +26,11 @@ dotenv = "0.15.0"
|
|||||||
listenfd = "0.3.0"
|
listenfd = "0.3.0"
|
||||||
tracing = { version = "0.1.0", features = ["log"] }
|
tracing = { version = "0.1.0", features = ["log"] }
|
||||||
tracing-subscriber = { version = "0.3.0", features = ["std", "env-filter", "fmt", "json"] }
|
tracing-subscriber = { version = "0.3.0", features = ["std", "env-filter", "fmt", "json"] }
|
||||||
tracing-bunyan-formatter = "0.3.0"
|
|
||||||
tracing-log = "0.1.0"
|
tracing-log = "0.1.0"
|
||||||
tracing-actix-web = "=0.5.0-beta.9"
|
tracing-actix-web = "=0.5.0-beta.9"
|
||||||
|
regex = "1.5.0"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
once_cell = "1.0"
|
once_cell = "1.0"
|
||||||
reqwest = "0.11.0"
|
reqwest = { version = "0.11.0", features = ["json"] }
|
||||||
|
|||||||
@@ -43,3 +43,7 @@ pub fn export(filters: Vec<&str>) -> Result<Vec<Task>, Error> {
|
|||||||
.collect();
|
.collect();
|
||||||
tasks
|
tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add(add_args: Vec<&str>) -> Result<u64, Error> {
|
||||||
|
taskwarrior::add(add_args)
|
||||||
|
}
|
||||||
|
|||||||
31
src/lib.rs
31
src/lib.rs
@@ -1,29 +1,51 @@
|
|||||||
use actix_web::{dev::Server, middleware, web, App, HttpResponse, HttpServer};
|
use actix_web::{dev::Server, middleware, web, App, HttpResponse, HttpServer};
|
||||||
use listenfd::ListenFd;
|
use listenfd::ListenFd;
|
||||||
use serde::Deserialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::Error;
|
use std::io::Error;
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use tracing_actix_web::TracingLogger;
|
use tracing_actix_web::TracingLogger;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate lazy_static;
|
||||||
|
|
||||||
pub mod contextswitch;
|
pub mod contextswitch;
|
||||||
pub mod observability;
|
pub mod observability;
|
||||||
pub mod taskwarrior;
|
pub mod taskwarrior;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct TaskQuery {
|
struct TaskQuery {
|
||||||
filter: String,
|
filter: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tracing::instrument(level = "debug", skip(task_query))]
|
#[derive(Deserialize, Serialize)]
|
||||||
|
pub struct TaskDefinition {
|
||||||
|
pub definition: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(level = "debug", skip_all, fields(filter = %task_query.filter.as_ref().unwrap_or(&"".to_string())))]
|
||||||
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")
|
||||||
.body(serde_json::to_string(&tasks)?))
|
.body(serde_json::to_string(&tasks)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(level = "debug", skip_all, fields(definition = %task_definition.definition))]
|
||||||
|
async fn add_task(task_definition: web::Json<TaskDefinition>) -> Result<HttpResponse, Error> {
|
||||||
|
let task_id = contextswitch::add(task_definition.definition.split(' ').collect())?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok()
|
||||||
|
.content_type("application/json")
|
||||||
|
.body(json!({ "id": task_id }).to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
async fn health_check() -> HttpResponse {
|
async fn health_check() -> HttpResponse {
|
||||||
HttpResponse::Ok().finish()
|
HttpResponse::Ok().finish()
|
||||||
}
|
}
|
||||||
@@ -41,6 +63,7 @@ pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
|||||||
)
|
)
|
||||||
.route("/ping", web::get().to(health_check))
|
.route("/ping", web::get().to(health_check))
|
||||||
.route("/tasks", web::get().to(list_tasks))
|
.route("/tasks", web::get().to(list_tasks))
|
||||||
|
.route("/tasks", web::post().to(add_task))
|
||||||
})
|
})
|
||||||
.keep_alive(60)
|
.keep_alive(60)
|
||||||
.shutdown_timeout(60);
|
.shutdown_timeout(60);
|
||||||
|
|||||||
@@ -1,18 +1,20 @@
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use configparser::ini::Ini;
|
use configparser::ini::Ini;
|
||||||
|
use regex::Regex;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::io::Error;
|
use std::io::{Error, ErrorKind};
|
||||||
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)]
|
||||||
pub struct Task {
|
pub struct Task {
|
||||||
pub uuid: Uuid,
|
pub uuid: Uuid,
|
||||||
pub id: u32,
|
pub id: u64,
|
||||||
#[serde(with = "contextswitch_types::tw_date_format")]
|
#[serde(with = "contextswitch_types::tw_date_format")]
|
||||||
pub entry: DateTime<Utc>,
|
pub entry: DateTime<Utc>,
|
||||||
#[serde(with = "contextswitch_types::tw_date_format")]
|
#[serde(with = "contextswitch_types::tw_date_format")]
|
||||||
@@ -44,29 +46,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();
|
let mut taskrc = Ini::new();
|
||||||
taskrc
|
taskrc.setstr("default", "data.location", Some(data_location));
|
||||||
.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();
|
|
||||||
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,14 +61,46 @@ 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()
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
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 args = [filters, vec!["export"]].concat();
|
||||||
args.extend(filters);
|
|
||||||
let export_output = Command::new("task").args(args).output()?;
|
let export_output = Command::new("task").args(args).output()?;
|
||||||
|
|
||||||
let tasks: Vec<Task> = serde_json::from_slice(&export_output.stdout)?;
|
let tasks: Vec<Task> = serde_json::from_slice(&export_output.stdout)?;
|
||||||
@@ -94,10 +108,32 @@ pub fn export(filters: Vec<&str>) -> Result<Vec<Task>, Error> {
|
|||||||
Ok(tasks)
|
Ok(tasks)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(add_args: Vec<&str>) -> Result<(), Error> {
|
#[tracing::instrument(level = "debug")]
|
||||||
|
pub fn add(add_args: Vec<&str>) -> Result<u64, 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();
|
||||||
Ok(())
|
lazy_static! {
|
||||||
|
static ref RE: Regex = Regex::new(r"Created task (?P<id>\d+).").unwrap();
|
||||||
|
}
|
||||||
|
let task_id_capture = RE.captures(&output).ok_or_else(|| {
|
||||||
|
Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("Cannot extract task ID from: {}", &output),
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
let task_id_str = task_id_capture
|
||||||
|
.name("id")
|
||||||
|
.ok_or_else(|| {
|
||||||
|
Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("Cannot extract task ID value from: {}", &output),
|
||||||
|
)
|
||||||
|
})?
|
||||||
|
.as_str();
|
||||||
|
|
||||||
|
task_id_str
|
||||||
|
.parse::<u64>()
|
||||||
|
.map_err(|_| Error::new(ErrorKind::Other, "Cannot parse task ID value"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,53 @@
|
|||||||
pub mod test_helper;
|
pub mod test_helper;
|
||||||
|
|
||||||
use contextswitch_api::taskwarrior;
|
use contextswitch_api::{taskwarrior, TaskDefinition};
|
||||||
use contextswitch_types::Task;
|
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 client = reqwest::Client::new();
|
let task_id =
|
||||||
taskwarrior::add(vec!["test1", "contextswitch:'{\"test\": 1}'"]).unwrap();
|
taskwarrior::add(vec!["test", "list_tasks", "contextswitch:'{\"test\": 1}'"]).unwrap();
|
||||||
|
|
||||||
let response: reqwest::Response = client
|
let tasks: Vec<Task> = reqwest::Client::new()
|
||||||
.get(&format!("{}/tasks?filter=ls", &address))
|
.get(&format!("{}/tasks?filter={}", &address, task_id))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.expect("Failed to execute request.");
|
.expect("Failed to execute request")
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.expect("Cannot parse JSON result");
|
||||||
|
|
||||||
test_helper::clear_tasks(task_data_path);
|
|
||||||
let text_body = response.text_with_charset("utf-8").await.unwrap();
|
|
||||||
let tasks: Vec<Task> = serde_json::from_str(&text_body).unwrap();
|
|
||||||
assert_eq!(tasks.len(), 1);
|
assert_eq!(tasks.len(), 1);
|
||||||
assert_eq!(tasks[0].description, "test1");
|
assert_eq!(tasks[0].description, "test list_tasks");
|
||||||
|
|
||||||
let cs_metadata = tasks[0].contextswitch.as_ref().unwrap();
|
let cs_metadata = tasks[0].contextswitch.as_ref().unwrap();
|
||||||
assert_eq!(cs_metadata.test, 1);
|
assert_eq!(cs_metadata.test, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn add_task() {
|
||||||
|
let address = test_helper::spawn_app();
|
||||||
|
println!("add_task address: {}", address);
|
||||||
|
|
||||||
|
let response: serde_json::Value = reqwest::Client::new()
|
||||||
|
.post(&format!("{}/tasks", &address))
|
||||||
|
.json(&TaskDefinition {
|
||||||
|
definition: "test add_task contextswitch:{\"test\":1}".to_string(),
|
||||||
|
})
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request")
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.expect("Cannot parse JSON result");
|
||||||
|
let new_task_id = response["id"].as_u64().unwrap();
|
||||||
|
let tasks = taskwarrior::export(vec![&new_task_id.to_string()]).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(tasks.len(), 1);
|
||||||
|
assert_eq!(tasks[0].id, new_task_id);
|
||||||
|
assert_eq!(tasks[0].description, "test add_task");
|
||||||
|
assert_eq!(
|
||||||
|
tasks[0].contextswitch.as_ref().unwrap(),
|
||||||
|
&"{\"test\":1}".to_string()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,23 +10,31 @@ static TRACING: Lazy<()> = Lazy::new(|| {
|
|||||||
init_subscriber(subscriber);
|
init_subscriber(subscriber);
|
||||||
});
|
});
|
||||||
|
|
||||||
pub fn spawn_app() -> String {
|
static SERVER_ADDRESS: Lazy<String> = Lazy::new(|| {
|
||||||
Lazy::force(&TRACING);
|
|
||||||
|
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
|
||||||
let port = listener.local_addr().unwrap().port();
|
let port = listener.local_addr().unwrap().port();
|
||||||
|
|
||||||
let server = contextswitch_api::run(listener).expect("Failed to bind address");
|
let server = contextswitch_api::run(listener).expect("Failed to bind address");
|
||||||
let _ = tokio::spawn(server);
|
let _ = tokio::spawn(server);
|
||||||
format!("http://127.0.0.1:{}", port)
|
format!("http://127.0.0.1:{}", port)
|
||||||
}
|
});
|
||||||
|
|
||||||
pub fn setup_tasks() -> String {
|
static TASK_DATA_LOCATION: Lazy<String> = Lazy::new(|| {
|
||||||
let tmp_dir = Temp::new_dir().unwrap();
|
let tmp_dir = Temp::new_dir().unwrap();
|
||||||
let task_data_location = taskwarrior::load_config(tmp_dir.to_str());
|
let task_data_location = taskwarrior::load_config(tmp_dir.to_str());
|
||||||
tmp_dir.release();
|
tmp_dir.release();
|
||||||
|
|
||||||
return task_data_location;
|
task_data_location
|
||||||
|
});
|
||||||
|
|
||||||
|
pub fn spawn_app() -> String {
|
||||||
|
Lazy::force(&TRACING);
|
||||||
|
setup_tasks();
|
||||||
|
Lazy::force(&SERVER_ADDRESS).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn setup_tasks() -> String {
|
||||||
|
Lazy::force(&TASK_DATA_LOCATION).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_tasks(task_data_location: String) {
|
pub fn clear_tasks(task_data_location: String) {
|
||||||
|
|||||||
Reference in New Issue
Block a user