Skip to content

Commit 32aafd5

Browse files
committed
tasks: add run_mode tests
1 parent 3c5253c commit 32aafd5

File tree

1 file changed

+73
-39
lines changed

1 file changed

+73
-39
lines changed

devenv-tasks/src/lib.rs

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Display for Error {
6666
}
6767
}
6868

69-
#[derive(Debug, Deserialize, Serialize)]
69+
#[derive(Clone, Debug, Deserialize, Serialize)]
7070
pub struct TaskConfig {
7171
name: String,
7272
#[serde(default)]
@@ -95,7 +95,7 @@ pub enum RunMode {
9595
All,
9696
}
9797

98-
#[derive(Deserialize, Serialize)]
98+
#[derive(Clone, Debug, Deserialize, Serialize)]
9999
pub struct Config {
100100
pub tasks: Vec<TaskConfig>,
101101
pub roots: Vec<String>,
@@ -1223,10 +1223,8 @@ mod test {
12231223
)
12241224
.await;
12251225
if let Err(Error::CycleDetected(_)) = result {
1226+
// The source of the cycle can be either task.
12261227
Ok(())
1227-
// TODO: we seem to detect the cycle in both tasks, non-deterministically.
1228-
// Investigate if this is expected, or if something changed.
1229-
// assert_eq!(task, "myapp:task_2".to_string());
12301228
} else {
12311229
Err(Error::TaskNotFound(format!(
12321230
"Expected Error::CycleDetected, got {:?}",
@@ -1354,45 +1352,81 @@ mod test {
13541352
}
13551353

13561354
#[tokio::test]
1357-
async fn test_single_task() -> Result<(), Error> {
1355+
async fn test_run_mode() -> Result<(), Error> {
13581356
let script1 = create_basic_script("1")?;
13591357
let script2 = create_basic_script("2")?;
13601358
let script3 = create_basic_script("3")?;
13611359

1362-
let tasks = Tasks::new(
1363-
Config::try_from(json!({
1364-
"roots": ["myapp:task_2"],
1365-
"run_mode": "single",
1366-
"tasks": [
1367-
{
1368-
"name": "myapp:task_1",
1369-
"command": script1.to_str().unwrap(),
1370-
},
1371-
{
1372-
"name": "myapp:task_2",
1373-
"command": script2.to_str().unwrap(),
1374-
"before": ["myapp:task_3"],
1375-
"after": ["myapp:task_1"],
1376-
},
1377-
{
1378-
"name": "myapp:task_3",
1379-
"command": script3.to_str().unwrap()
1380-
}
1381-
]
1382-
}))
1383-
.unwrap(),
1384-
)
1385-
.await?;
1386-
tasks.run().await;
1360+
let config = Config::try_from(json!({
1361+
"roots": ["myapp:task_2"],
1362+
"run_mode": "single",
1363+
"tasks": [
1364+
{
1365+
"name": "myapp:task_1",
1366+
"command": script1.to_str().unwrap(),
1367+
},
1368+
{
1369+
"name": "myapp:task_2",
1370+
"command": script2.to_str().unwrap(),
1371+
"before": ["myapp:task_3"],
1372+
"after": ["myapp:task_1"],
1373+
},
1374+
{
1375+
"name": "myapp:task_3",
1376+
"command": script3.to_str().unwrap()
1377+
}
1378+
]
1379+
}))
1380+
.unwrap();
13871381

1388-
let task_statuses = inspect_tasks(&tasks).await;
1389-
let task_statuses = task_statuses.as_slice();
1390-
assert_matches!(
1391-
task_statuses,
1392-
[
1393-
(name2, TaskStatus::Completed(TaskCompleted::Success(_, _))),
1394-
] if name2 == "myapp:task_2"
1395-
);
1382+
// Single task
1383+
{
1384+
let tasks = Tasks::new(config.clone()).await?;
1385+
tasks.run().await;
1386+
1387+
let task_statuses = inspect_tasks(&tasks).await;
1388+
let task_statuses = task_statuses.as_slice();
1389+
assert_matches!(
1390+
task_statuses,
1391+
[
1392+
(name2, TaskStatus::Completed(TaskCompleted::Success(_, _))),
1393+
] if name2 == "myapp:task_2"
1394+
);
1395+
}
1396+
1397+
// Before tasks
1398+
{
1399+
let mut config = config.clone();
1400+
config.run_mode = RunMode::Before;
1401+
let tasks = Tasks::new(config).await?;
1402+
tasks.run().await;
1403+
let task_statuses = inspect_tasks(&tasks).await;
1404+
let task_statuses = task_statuses.as_slice();
1405+
assert_matches!(
1406+
task_statuses,
1407+
[
1408+
(name1, TaskStatus::Completed(TaskCompleted::Success(_, _))),
1409+
(name2, TaskStatus::Completed(TaskCompleted::Success(_, _))),
1410+
] if name1 == "myapp:task_1" && name2 == "myapp:task_2"
1411+
);
1412+
}
1413+
1414+
// After tasks
1415+
{
1416+
let mut config = config.clone();
1417+
config.run_mode = RunMode::After;
1418+
let tasks = Tasks::new(config).await?;
1419+
tasks.run().await;
1420+
let task_statuses = inspect_tasks(&tasks).await;
1421+
let task_statuses = task_statuses.as_slice();
1422+
assert_matches!(
1423+
task_statuses,
1424+
[
1425+
(name2, TaskStatus::Completed(TaskCompleted::Success(_, _))),
1426+
(name3, TaskStatus::Completed(TaskCompleted::Success(_, _))),
1427+
] if name2 == "myapp:task_2" && name3 == "myapp:task_3"
1428+
);
1429+
}
13961430

13971431
Ok(())
13981432
}

0 commit comments

Comments
 (0)