use crate::prelude::*;
use cargo_test_support::project;
use cargo_test_support::str;

#[cargo_test]
fn bin_name_explicit() {
    let p = project()
        .file(
            "Cargo.toml",
            r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []

[[bin]]
name = "foo_bar"
path = "src/main.rs"

[lints.cargo]
default = { level = "allow", priority = -1 }
non_kebab_case_bins = "warn"
"#,
        )
        .file("src/main.rs", "fn main() {}")
        .build();

    p.cargo("fetch -Zcargo-lints")
        .masquerade_as_nightly_cargo(&["cargo-lints"])
        .with_stderr_data(str![[r#"
[WARNING] binaries should have a kebab-case name
  |
1 | [ROOT]/foo/target/.../foo_bar[EXE]
  |                   [..]^^^^^^^
  |
  = [NOTE] `cargo::non_kebab_case_bins` is set to `warn` in `[lints]`
[HELP] to change the binary name to kebab case, convert `bin.name`
 --> Cargo.toml:9:8
  |
9 - name = "foo_bar"
9 + name = "foo-bar"
  |
[WARNING] `foo` (manifest) generated 1 warning

"#]])
        .run();
}

#[cargo_test]
fn bin_name_from_package() {
    let p = project()
        .file(
            "Cargo.toml",
            r#"
[package]
name = "foo_bar"
version = "0.0.1"
edition = "2015"
authors = []

[lints.cargo]
default = { level = "allow", priority = -1 }
non_kebab_case_bins = "warn"
"#,
        )
        .file("src/main.rs", "fn main() {}")
        .build();

    p.cargo("fetch -Zcargo-lints")
        .masquerade_as_nightly_cargo(&["cargo-lints"])
        .with_stderr_data(str![[r#"
[WARNING] binaries should have a kebab-case name
   |
 1 | [ROOT]/foo/target/.../foo_bar[EXE]
   |                   [..]^^^^^^^
   |
   = [NOTE] `cargo::non_kebab_case_bins` is set to `warn` in `[lints]`
[HELP] to change the binary name to kebab case, convert `package.name`
  --> Cargo.toml:3:8
   |
 3 - name = "foo_bar"
 3 + name = "foo-bar"
   |
[HELP] to change the binary name to kebab case, specify `bin.name`
  --> Cargo.toml:10:30
   |
10 ~ non_kebab_case_bins = "warn"
11 + [[bin]]
12 + name = "foo-bar"
13 + path = "src/main.rs"
   |
[WARNING] `foo_bar` (manifest) generated 1 warning

"#]])
        .run();
}

#[cargo_test]
fn bin_name_from_path() {
    let p = project()
        .file(
            "Cargo.toml",
            r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
authors = []

[lints.cargo]
default = { level = "allow", priority = -1 }
non_kebab_case_bins = "warn"
"#,
        )
        .file("src/bin/foo_bar.rs", "fn main() {}")
        .build();

    p.cargo("fetch -Zcargo-lints")
        .masquerade_as_nightly_cargo(&["cargo-lints"])
        .with_stderr_data(str![[r#"
[WARNING] binaries should have a kebab-case name
  |
1 | [ROOT]/foo/target/.../foo_bar[EXE]
  |                   [..]^^^^^^^
  |
  = [NOTE] `cargo::non_kebab_case_bins` is set to `warn` in `[lints]`
[HELP] to change the binary name to kebab case, convert the file stem
  |
1 - src/bin/foo_bar.rs
1 + src/bin/foo-bar.rs
  |
[WARNING] `foo` (manifest) generated 1 warning

"#]])
        .run();
}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
fn bin_name_from_script_name() {
    let p = cargo_test_support::project()
        .file(
            "foo_bar",
            r#"
---
[lints.cargo]
default = { level = "allow", priority = -1 }
non_kebab_case_bins = "warn"
---
fn main() {}"#,
        )
        .build();

    p.cargo("fetch -Zcargo-lints -Zscript --manifest-path foo_bar")
        .masquerade_as_nightly_cargo(&["cargo-lints", "script"])
        .with_stderr_data(str![[r#"
[WARNING] `package.edition` is unspecified, defaulting to the latest edition (currently `[..]`)
[HELP] to pin the edition, run `cargo fix --manifest-path [ROOT]/foo/foo_bar`
[WARNING] binaries should have a kebab-case name
  |
1 | [ROOT]/home/.cargo/build/[HASH]/target/.../foo_bar[EXE]
  |                                        [..]^^^^^^^
  |
  = [NOTE] `cargo::non_kebab_case_bins` is set to `warn` in `[lints]`
[HELP] to change the binary name to kebab case, convert the file stem
  |
1 - foo_bar
1 + foo-bar
  |
[WARNING] `foo_bar` (manifest) generated 1 warning

"#]])
        .run();
}
