Skip to content

Commit

Permalink
Merge pull request #158 from qryxip/overwrite-flag
Browse files Browse the repository at this point in the history
Add `--overwrite` flag
  • Loading branch information
qryxip authored Mar 31, 2021
2 parents 4807135 + cf9ae88 commit b45f699
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 3 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@
##language_id = ""
```

- Now `download` command requires `--overwrite` flag to overwrite existing test files.

```console
❯ cargo compete d
error: `/home/ryo/src/local/competitive/atcoder/arc115/testcases/a.yml` already exists. run with `--overwrite` to overwrite
❯ cargo compete d --overwrite
Saved 2 test cases to /home/ryo/src/local/competitive/atcoder/arc115/testcases/{a.yml, a/}
Saved 2 test cases to /home/ryo/src/local/competitive/atcoder/arc115/testcases/{b.yml, b/}
Saved 1 test case to /home/ryo/src/local/competitive/atcoder/arc115/testcases/{c.yml, c/}
Saved 2 test cases to /home/ryo/src/local/competitive/atcoder/arc115/testcases/{d.yml, d/}
Saved 2 test cases to /home/ryo/src/local/competitive/atcoder/arc115/testcases/{e.yml, e/}
Saved 5 test cases to /home/ryo/src/local/competitive/atcoder/arc115/testcases/{f.yml, f/}
```

## [0.8.8] - 2021-03-10Z

### Added
Expand Down
2 changes: 1 addition & 1 deletion README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ bin-name = '{% assign segments = url | split: "/" %}{{ segments[5] }}'
**パッケージを対象に取ります。**
パッケージに`cd`して実行してください。

![Screenshot](https://user-images.githubusercontent.com/14125495/91647644-06e7bb00-ea98-11ea-8bc6-cd57714e4c84.png)
![Screenshot](https://user-images.githubusercontent.com/14125495/113158161-82039080-9276-11eb-89df-58613b276ba4.png)

プラットフォームが使っているテストケースを公開している場合、`--full`を指定することでそちらをダウンロードすることができます。

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Retrieves test cases for an existing package.
**This is a command for a package.**
`cd` to the package generated with [`cargo compete new`](#cargo-compete-new).

![Screenshot](https://user-images.githubusercontent.com/14125495/91647644-06e7bb00-ea98-11ea-8bc6-cd57714e4c84.png)
![Screenshot](https://user-images.githubusercontent.com/14125495/113158161-82039080-9276-11eb-89df-58613b276ba4.png)

With `--open` option, you can download system test cases instead of sample ones.

Expand Down
1 change: 1 addition & 0 deletions src/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ pub(crate) fn run(opt: OptCompeteAdd, ctx: crate::Context<'_>) -> anyhow::Result
&metadata.workspace_root,
member.manifest_dir(),
&cargo_compete_config.test_suite,
true,
problems,
|url, _| vec![bin_names_by_url[url].clone()],
|url, _| vec![bin_aliases_by_url[url].clone()],
Expand Down
4 changes: 4 additions & 0 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
&cargo_compete_dir,
&manifest_dir,
&cargo_compete_config.test_suite,
true,
outcome,
|_, index| vec![group.package_name() + "-" + &index.to_kebab_case()],
|_, index| vec![index.to_kebab_case()],
Expand Down Expand Up @@ -167,6 +168,7 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
&cargo_compete_dir,
&manifest_dir,
&cargo_compete_config.test_suite,
true,
outcome,
|_, index| vec![group.package_name() + "-" + &index.to_kebab_case()],
|_, index| vec![index.to_kebab_case()],
Expand Down Expand Up @@ -233,6 +235,7 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
&cargo_compete_dir,
&manifest_dir,
&cargo_compete_config.test_suite,
true,
outcome,
|_, index| vec![group.package_name() + "-" + &index.to_kebab_case()],
|_, index| vec![index.to_kebab_case()],
Expand Down Expand Up @@ -304,6 +307,7 @@ pub fn run(opt: OptCompeteNew, ctx: crate::Context<'_>) -> anyhow::Result<()> {
&cargo_compete_dir,
&manifest_dir,
&cargo_compete_config.test_suite,
true,
problems,
|_, index| vec![group.package_name() + "-" + &index.to_kebab_case()],
|_, index| vec![index.to_kebab_case()],
Expand Down
1 change: 1 addition & 0 deletions src/commands/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub(crate) fn run(opt: OptCompeteOpen, ctx: crate::Context<'_>) -> anyhow::Resul
Some(missing_bins),
Some(missing_examples),
full,
true,
&metadata.workspace_root,
&cargo_compete_config.test_suite,
&cookies_path,
Expand Down
6 changes: 6 additions & 0 deletions src/commands/retrieve_testcases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub struct OptCompeteRetrieveTestcases {
#[structopt(long)]
pub full: bool,

/// Overwrites the existing test files
#[structopt(long)]
pub overwrite: bool,

/// Retrieve only the problems for the binary target
#[structopt(long, value_name("NAME_OR_ALIAS"))]
pub bin: Option<Vec<String>>,
Expand Down Expand Up @@ -41,6 +45,7 @@ pub struct OptCompeteRetrieveTestcases {
pub(crate) fn run(opt: OptCompeteRetrieveTestcases, ctx: crate::Context<'_>) -> anyhow::Result<()> {
let OptCompeteRetrieveTestcases {
full,
overwrite,
bin,
example,
package,
Expand Down Expand Up @@ -76,6 +81,7 @@ pub(crate) fn run(opt: OptCompeteRetrieveTestcases, ctx: crate::Context<'_>) ->
bin,
example,
full,
overwrite,
&metadata.workspace_root,
&cargo_compete_config.test_suite,
&cookies_path,
Expand Down
12 changes: 11 additions & 1 deletion src/web/retrieve_testcases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
shell::Shell,
web::credentials,
};
use anyhow::Context;
use anyhow::{ensure, Context};
use camino::{Utf8Path, Utf8PathBuf};
use cargo_metadata as cm;
use indexmap::{indexmap, IndexMap};
Expand Down Expand Up @@ -111,6 +111,7 @@ pub(crate) fn dl_for_existing_package(
bin_name_aliases: Option<&HashSet<String>>,
example_name_aliases: Option<&HashSet<String>>,
full: bool,
overwrite: bool,
workspace_root: &Utf8Path,
test_suite_path: &liquid::Template,
cookies_path: &Path,
Expand Down Expand Up @@ -186,6 +187,7 @@ pub(crate) fn dl_for_existing_package(
workspace_root,
package.manifest_dir(),
test_suite_path,
overwrite,
outcome,
|url, _| {
snowchains_targets
Expand Down Expand Up @@ -319,10 +321,12 @@ pub(crate) fn system_test_cases_dir(problem_url: &Url) -> anyhow::Result<PathBuf
.fold(system_test_cases_dir, |d, p| d.join(p)))
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn save_test_cases<I>(
workspace_root: &Utf8Path,
pkg_manifest_dir: &Utf8Path,
path: &liquid::Template,
overwrite: bool,
problems: Vec<Problem<I>>,
bin_names: impl Fn(&Url, &I) -> Vec<String>,
bin_aliases: impl Fn(&Url, &I) -> Vec<String>,
Expand Down Expand Up @@ -385,6 +389,12 @@ pub(crate) fn save_test_cases<I>(
shell,
)?;

ensure!(
overwrite || !path.exists(),
"`{}` already exists. run with `--overwrite` to overwrite",
path,
);

acc.push(path.clone());

if !empty {
Expand Down

0 comments on commit b45f699

Please sign in to comment.