Include: include/setup.vader

Execute (rust: cargo: handles code correctly):
  let null = g:neomake#compat#json_null
  let true = g:neomake#compat#json_true

  let json = join([
  \ '{"reason":"compiler-message"',
  \ '"package_id":"inko 0.3.0 (path+file:///home/yorickpeterse/Projects/inko/inko/vm)"',
  \ '"target":{"kind":["lib"]',
  \   '"crate_types":["lib"]',
  \   '"name":"libinko"',
  \   '"src_path":"/home/yorickpeterse/Projects/inko/inko/vm/src/lib.rs"',
  \   '"edition":"2015"}',
  \ '"message":{"message":"field is never used: `stealer`"',
  \   '"code":{"code":"dead_code","explanation":null}',
  \   '"level":"warning"',
  \   '"spans":[{"file_name":"src/scheduling/scheduler.rs","byte_start":83,"byte_end":102,"line_start":4,"line_end":4,"column_start":5,"column_end":24,"is_primary":true,"text":[{"text":"    stealer: Stealer<T>,","highlight_start":5,"highlight_end":24}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}]',
  \   '"children":[{"message":"#[warn(dead_code)] on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null}]',
  \   '"rendered":"warning: field is never used: `stealer`\\n --> src/scheduling/scheduler.rs:4:5\\n  |\\n4 |     stealer: Stealer<T>,\\n  |     ^^^^^^^^^^^^^^^^^^^\\n  |\\n  = note: #[warn(dead_code)] on by default\\n\\n"}}',
  \ ], ',')

  let context = {'output': [json]}
  let errors = neomake#makers#ft#rust#CargoProcessOutput(context)
  AssertEqual errors, [
  \ {'lnum': 4, 'col': 5, 'filename': 'src/scheduling/scheduler.rs',
  \  'type': 'W',
  \  'maker_name': 'cargo', 'length': 19,
  \  'text': 'field is never used: `stealer`. #[warn(dead_code)] on by default'}]

Execute (cargo skip non json line):
  new
  file build/lib.rs
  let cargo = neomake#makers#ft#rust#cargo()
  let cargo.exe = 'printf'
  let cargo.args = ['error: Could not compile `derive_more`.']

  CallNeomake 1, [cargo]
  AssertEqual [], getloclist(0)
  bwipe

Execute (cargo info message):
  new
  file build/lib.rs
  let cargo = NeomakeTestsGetMakerWithOutput(neomake#makers#ft#rust#cargo(),
    \ '../tests/fixtures/rust/cargo_info.json')

  CallNeomake 1, [cargo]
  AssertEqual [], getloclist(0)
  bwipe

Execute (cargo error without span):
  new
  file build/lib.rs
  let cargo = NeomakeTestsGetMakerWithOutput(neomake#makers#ft#rust#cargo(),
    \ '../tests/fixtures/rust/cargo_error_without_span.json')

  CallNeomake 1, [cargo]
  AssertEqual [], getloclist(0)
  bwipe

Execute (cargo: uses primary span):
  new
  file build/main.rs
  let cargo = NeomakeTestsGetMakerWithOutput(neomake#makers#ft#rust#cargo(),
    \ 'tests/fixtures/rust/cargo_error_primary_span.json')
  let cargo.cwd = getcwd()

  CallNeomake 1, [cargo]

  AssertEqualQf getloclist(0), [{
  \ 'lnum': 6, 'bufnr': bufnr('%'), 'col': 5, 'valid': 1, 'vcol': 0, 'nr': 61,
  \ 'type': 'E', 'pattern': '',
  \ 'text': 'this function takes 1 parameter but 2 parameters were supplied: expected 1 parameter',
  \ }]
  bwipe

Execute (rust: cargo: uses directory with Cargo.toml as cwd):
  new
  let maker = neomake#makers#ft#rust#cargo()
  Assert !has_key(maker, 'cwd')
  let maker = maker.InitForJob(NeomakeTestsFakeJobinfo())
  AssertEqual maker.cwd, '%:p:h'
  AssertNeomakeMessage "Failed to get cargo metadata for workspace using 'cargo metadata --no-deps --format-version 1'.", 3

  let tempdir = tempname()
  let slash = neomake#utils#Slash()
  let subdir = tempdir . slash . 'subdir'
  call mkdir(subdir, 'p')
  let cargo_toml = tempdir . slash . 'Cargo.toml'
  call writefile([], cargo_toml)

  exe 'lcd' subdir
  let maker = neomake#makers#ft#rust#cargo()
  let maker = maker.InitForJob(NeomakeTestsFakeJobinfo())
  AssertEqual maker.cwd, tempdir
  bwipe

Execute (rust: cargo: uses cargo workspace_root as cwd):
  new
  call g:NeomakeTestsCreateExe('cargo', [
  \ 'echo ''{"workspace_root": "/cargo_workspace_root"}'''])
  let maker = neomake#makers#ft#rust#cargo()
  let maker = maker.InitForJob(NeomakeTestsFakeJobinfo())
  AssertEqual maker.cwd, '/cargo_workspace_root'
  bwipe

Execute (rust: cargo: handles cd'ing before querying workspace_root metadata):
  new
  let tempdir = tempname()
  call mkdir(tempdir)
  exe printf('file %s/fake.rs', fnameescape(tempdir))

  call g:NeomakeTestsCreateExe('cargo', [
  \ printf('[ "$PWD" = "%s" ] && echo ''{"workspace_root": "%s"}''',
  \ tempdir, tempdir)])
  let maker = neomake#makers#ft#rust#cargo()
  let maker = maker.InitForJob(NeomakeTestsFakeJobinfo())
  AssertEqual maker.cwd, tempdir
  bwipe

Execute (rust: cargo: logs error when cd'ing fails):
  new
  file /doesnotexist/fake.rs

  let maker = neomake#makers#ft#rust#cargo()
  let maker = maker.InitForJob(NeomakeTestsFakeJobinfo())
  AssertNeomakeMessage '\Vs:get_cargo_workspace_root: failed to cd to buffer directory: Vim(cd):E344: ', 3
  AssertEqual maker.cwd, '%:p:h'
  bwipe

Execute (rust: cargotest detect failures):
  new
  file src/lib.rs
  let cargotest = NeomakeTestsGetMakerWithOutput(neomake#makers#ft#rust#cargotest(),
    \ 'tests/fixtures/rust/cargotest_with_failures')
  let cargotest.cwd = getcwd()

  CallNeomake 1, [cargotest]

  AssertEqual map(getloclist(0), '[v:val.type, v:val.valid, v:val.text]'), [
    \ ['E', 1, 'failed to resolve. Maybe a missing `extern crate non;`?'],
    \ ['W', 1, 'unused import: `non::existent::module`'],
    \ ]
  bwipe

Execute (rust: cargotest: uses directory with Cargo.toml as cwd):
  let maker = neomake#makers#ft#rust#cargotest()

  let tempdir = tempname()
  let slash = neomake#utils#Slash()
  let subdir = tempdir . slash . 'subdir'
  call mkdir(subdir, 'p')
  let cargo_toml = tempdir . slash . 'Cargo.toml'
  call writefile([], cargo_toml)

  new
  exe 'lcd' subdir
  let maker = neomake#makers#ft#rust#cargotest()
  let maker = maker.InitForJob(NeomakeTestsFakeJobinfo())
  AssertEqual maker.cwd, tempdir
  bwipe
