/// A custom step using hk's whitespace utility, including check and fix tests.
amends "package://github.com/jdx/hk/releases/download/v1.58.1/hk@1.58.1#/Config.pkl"

import "package://github.com/jdx/hk/releases/download/v1.58.1/hk@1.58.1#/Builtins.pkl"

local linters = new Mapping<String, Step> {
  ["whitespace"] {
    glob = List("*.txt", "*.md")
    check = "hk util trailing-whitespace {{files}}"
    fix = "hk util trailing-whitespace --fix {{files}}"
    tests {
      ["accepts clean text"] {
        run = "check"
        files = List("{{tmp}}/clean.txt")
        write {
          ["{{tmp}}/clean.txt"] = "hello\n"
        }
        expect { code = 0 }
      }
      ["removes trailing spaces"] {
        run = "fix"
        files = List("{{tmp}}/dirty.txt")
        write {
          ["{{tmp}}/dirty.txt"] = "hello  \n"
        }
        expect {
          files {
            ["{{tmp}}/dirty.txt"] = "hello\n"
          }
        }
      }
    }
  }
  ["newlines"] = Builtins.newlines
}

hooks {
  ["pre-commit"] {
    fix = true
    stash = "git"
    steps = linters
  }
  ["check"] { steps = linters }
  ["fix"] {
    fix = true
    steps = linters
  }
}
