Skip to content

Commit b83e3c7

Browse files
committed
Auto merge of #385 - pietroalbini:action-trait, r=pietroalbini
Refactor actions to implement the Action trait
2 parents ac183ef + 8346d3b commit b83e3c7

File tree

12 files changed

+156
-135
lines changed

12 files changed

+156
-135
lines changed

src/actions/experiments/create.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::actions::experiments::ExperimentError;
2-
use crate::config::Config;
3-
use crate::db::{Database, QueryUtils};
1+
use crate::actions::{experiments::ExperimentError, Action, ActionsCtx};
2+
use crate::db::QueryUtils;
43
use crate::experiments::{CapLints, CrateSelect, Experiment, GitHubIssue, Mode, Status};
54
use crate::prelude::*;
65
use crate::toolchain::Toolchain;
@@ -33,21 +32,23 @@ impl CreateExperiment {
3332
ignore_blacklist: false,
3433
}
3534
}
35+
}
3636

37-
pub fn apply(self, db: &Database, config: &Config) -> Fallible<()> {
37+
impl Action for CreateExperiment {
38+
fn apply(self, ctx: &ActionsCtx) -> Fallible<()> {
3839
// Ensure no duplicate experiments are created
39-
if Experiment::exists(db, &self.name)? {
40-
return Err(ExperimentError::AlreadyExists(self.name).into());
40+
if Experiment::exists(&ctx.db, &self.name)? {
41+
return Err(ExperimentError::AlreadyExists(self.name.clone()).into());
4142
}
4243

4344
// Ensure no experiment with duplicate toolchains is created
4445
if self.toolchains[0] == self.toolchains[1] {
4546
return Err(ExperimentError::DuplicateToolchains.into());
4647
}
4748

48-
let crates = crate::crates::lists::get_crates(self.crates, db, config)?;
49+
let crates = crate::crates::lists::get_crates(self.crates, &ctx.db, &ctx.config)?;
4950

50-
db.transaction(|transaction| {
51+
ctx.db.transaction(|transaction| {
5152
transaction.execute(
5253
"INSERT INTO experiments \
5354
(name, mode, cap_lints, toolchain_start, toolchain_end, priority, created_at, \
@@ -70,7 +71,7 @@ impl CreateExperiment {
7071
)?;
7172

7273
for krate in &crates {
73-
let skipped = !self.ignore_blacklist && config.should_skip(krate);
74+
let skipped = !self.ignore_blacklist && ctx.config.should_skip(krate);
7475
transaction.execute(
7576
"INSERT INTO experiment_crates (experiment, crate, skipped) VALUES (?1, ?2, ?3);",
7677
&[&self.name, &::serde_json::to_string(&krate)?, &skipped],
@@ -87,7 +88,7 @@ impl CreateExperiment {
8788
#[cfg(test)]
8889
mod tests {
8990
use super::CreateExperiment;
90-
use crate::actions::ExperimentError;
91+
use crate::actions::{Action, ActionsCtx, ExperimentError};
9192
use crate::config::{Config, CrateConfig};
9293
use crate::crates::Crate;
9394
use crate::db::{Database, QueryUtils};
@@ -98,6 +99,7 @@ mod tests {
9899
fn test_creation() {
99100
let db = Database::temp().unwrap();
100101
let config = Config::default();
102+
let ctx = ActionsCtx::new(&db, &config);
101103

102104
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
103105

@@ -118,7 +120,7 @@ mod tests {
118120
}),
119121
ignore_blacklist: true,
120122
}
121-
.apply(&db, &config)
123+
.apply(&ctx)
122124
.unwrap();
123125

124126
let ex = Experiment::get(&db, "foo").unwrap().unwrap();
@@ -182,22 +184,23 @@ mod tests {
182184
broken: false,
183185
},
184186
);
187+
let ctx = ActionsCtx::new(&db, &config);
185188

186189
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
187190

188191
CreateExperiment {
189192
ignore_blacklist: false,
190193
..CreateExperiment::dummy("foo")
191194
}
192-
.apply(&db, &config)
195+
.apply(&ctx)
193196
.unwrap();
194197
assert!(is_skipped(&db, "foo", "build-pass"));
195198

196199
CreateExperiment {
197200
ignore_blacklist: true,
198201
..CreateExperiment::dummy("bar")
199202
}
200-
.apply(&db, &config)
203+
.apply(&ctx)
201204
.unwrap();
202205
assert!(!is_skipped(&db, "bar", "build-pass"));
203206
}
@@ -206,6 +209,7 @@ mod tests {
206209
fn test_duplicate_toolchains() {
207210
let db = Database::temp().unwrap();
208211
let config = Config::default();
212+
let ctx = ActionsCtx::new(&db, &config);
209213

210214
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
211215

@@ -220,7 +224,7 @@ mod tests {
220224
github_issue: None,
221225
ignore_blacklist: false,
222226
}
223-
.apply(&db, &config)
227+
.apply(&ctx)
224228
.unwrap_err();
225229

226230
assert_eq!(
@@ -233,6 +237,7 @@ mod tests {
233237
fn test_duplicate_name() {
234238
let db = Database::temp().unwrap();
235239
let config = Config::default();
240+
let ctx = ActionsCtx::new(&db, &config);
236241

237242
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
238243

@@ -247,7 +252,7 @@ mod tests {
247252
github_issue: None,
248253
ignore_blacklist: false,
249254
}
250-
.apply(&db, &config)
255+
.apply(&ctx)
251256
.unwrap();
252257

253258
// While the second one fails
@@ -261,7 +266,7 @@ mod tests {
261266
github_issue: None,
262267
ignore_blacklist: false,
263268
}
264-
.apply(&db, &config)
269+
.apply(&ctx)
265270
.unwrap_err();
266271

267272
assert_eq!(

src/actions/experiments/delete.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
use crate::actions::experiments::ExperimentError;
2-
use crate::config::Config;
3-
use crate::db::{Database, QueryUtils};
1+
use crate::actions::{experiments::ExperimentError, Action, ActionsCtx};
2+
use crate::db::QueryUtils;
43
use crate::experiments::Experiment;
54
use crate::prelude::*;
65

76
pub struct DeleteExperiment {
87
pub name: String,
98
}
109

11-
impl DeleteExperiment {
12-
pub fn apply(self, db: &Database, _config: &Config) -> Fallible<()> {
13-
if !Experiment::exists(db, &self.name)? {
10+
impl Action for DeleteExperiment {
11+
fn apply(self, ctx: &ActionsCtx) -> Fallible<()> {
12+
if !Experiment::exists(&ctx.db, &self.name)? {
1413
return Err(ExperimentError::NotFound(self.name).into());
1514
}
1615

1716
// This will also delete all the data related to this experiment, thanks to the foreign
1817
// keys in the SQLite database
19-
db.execute("DELETE FROM experiments WHERE name = ?1;", &[&self.name])?;
18+
ctx.db
19+
.execute("DELETE FROM experiments WHERE name = ?1;", &[&self.name])?;
2020

2121
Ok(())
2222
}
@@ -25,7 +25,7 @@ impl DeleteExperiment {
2525
#[cfg(test)]
2626
mod tests {
2727
use super::DeleteExperiment;
28-
use crate::actions::{CreateExperiment, ExperimentError};
28+
use crate::actions::{Action, ActionsCtx, CreateExperiment, ExperimentError};
2929
use crate::config::Config;
3030
use crate::db::Database;
3131
use crate::experiments::Experiment;
@@ -34,11 +34,12 @@ mod tests {
3434
fn test_delete_missing_experiment() {
3535
let db = Database::temp().unwrap();
3636
let config = Config::default();
37+
let ctx = ActionsCtx::new(&db, &config);
3738

3839
let err = DeleteExperiment {
3940
name: "dummy".to_string(),
4041
}
41-
.apply(&db, &config)
42+
.apply(&ctx)
4243
.unwrap_err();
4344

4445
assert_eq!(
@@ -51,20 +52,19 @@ mod tests {
5152
fn test_delete_experiment() {
5253
let db = Database::temp().unwrap();
5354
let config = Config::default();
55+
let ctx = ActionsCtx::new(&db, &config);
5456

5557
crate::crates::lists::setup_test_lists(&db, &config).unwrap();
5658

5759
// Create a dummy experiment and make sure it exists
58-
CreateExperiment::dummy("dummy")
59-
.apply(&db, &config)
60-
.unwrap();
60+
CreateExperiment::dummy("dummy").apply(&ctx).unwrap();
6161
assert!(Experiment::exists(&db, "dummy").unwrap());
6262

6363
// Delete it and make sure it doesn't exist anymore
6464
DeleteExperiment {
6565
name: "dummy".to_string(),
6666
}
67-
.apply(&db, &config)
67+
.apply(&ctx)
6868
.unwrap();
6969
assert!(!Experiment::exists(&db, "dummy").unwrap());
7070
}

0 commit comments

Comments
 (0)