diff options
| author | Leitwolf11 | 2026-11-13 20:34:58 +0300 |
|---|---|---|
| committer | Leitwolf11 | 2026-11-13 20:34:58 +0300 |
| commit | 3f1a5ace6dc7bfea240f07e3c6fb84de25e6c582 (patch) | |
| tree | 508ab499a33226c50a0ce8e44117cef8422d6870 | |
| parent | 3875d3db9770344da6dd668a8de6ec898e9cdb4d (diff) | |
| download | tazel-feature/fmt-lint-compile-commands.tar.gz | |
Add tazel fmt, tazel lint and the compilation databasefeature/fmt-lint-compile-commands
tazel fmt formats the files the selected targets own (sources less
excludes, the headers below their header directories, textual headers)
on every triple they are built for: clang-format for the four C-family
languages, zig fmt for Zig. --check reports and changes nothing.
tazel lint plans the selected targets for one triple and mode, stages
their headers, and writes compile_commands.json with the compile command
tazel build runs (less -MD -MF) to the output root and to
.tazel/compile_commands.json. It then runs clang-tidy on each C-family
translation unit of the selected targets in the job pool; --fix runs one
unit at a time so fixes to a shared header do not race. Zig targets are
skipped with a note.
--check belongs to fmt and --fix to lint; what follows -- goes to the
tool. The tests use the clang-format and clang-tidy of the machine and
return early without them.
| -rw-r--r-- | CHANGELOG.md | 7 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | sources/main/kotlin/com/progmasoft/tazel/cli/BuildCommands.kt | 30 | ||||
| -rw-r--r-- | sources/main/kotlin/com/progmasoft/tazel/cli/Main.kt | 14 | ||||
| -rw-r--r-- | sources/main/kotlin/com/progmasoft/tazel/cli/SourceCommands.kt | 86 | ||||
| -rw-r--r-- | sources/main/kotlin/com/progmasoft/tazel/driver/SourceTools.kt | 236 | ||||
| -rw-r--r-- | sources/test/kotlin/com/progmasoft/tazel/cli/SourceCommandTest.kt | 134 |
7 files changed, 501 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 38c4a4b..0defabd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,3 +29,10 @@ Versions follow [Semantic Versioning](https://semver.org). The earlier Tazel, 0. split into shards with `TEST_TOTAL_SHARDS` and `TEST_SHARD_INDEX`, stopped at its timeout (from `size` unless set), run up to three times when `flaky`, with tests tagged `EXCLUSIVE` alone and `MANUAL` ones only when named. A test that passed is not run again while its program, data, arguments and environment are the same, unless `NO_CACHE`. +- `tazel fmt` formats the sources, header directories and textual headers of the selected targets, on every triple + they are built for, with clang-format (C, C++, Objective-C, Objective-C++) and `zig fmt`; `--check` reports what is + not formatted and changes nothing. Each tool reads its own configuration, such as `.clang-format`. +- `tazel lint` writes the compilation database of the selected targets and their libraries to + `.tazel/out/<triple>/<mode>/compile_commands.json`, copies it to `.tazel/compile_commands.json`, and runs clang-tidy + on each C-family translation unit of the selected targets; `--fix` applies its fixes, one unit at a time. Zig + targets are skipped. For both commands, what follows `--` goes to the tool. @@ -21,6 +21,8 @@ cpp.bin("greeter") { tazel build //app/:greeter tazel run //app/:greeter tazel test //tests/... +tazel fmt --check //... +tazel lint //... ``` Tazel is under development; this repository starts its history again at 0.1.0. The earlier Tazel, the build system of diff --git a/sources/main/kotlin/com/progmasoft/tazel/cli/BuildCommands.kt b/sources/main/kotlin/com/progmasoft/tazel/cli/BuildCommands.kt index bace4a2..611096c 100644 --- a/sources/main/kotlin/com/progmasoft/tazel/cli/BuildCommands.kt +++ b/sources/main/kotlin/com/progmasoft/tazel/cli/BuildCommands.kt @@ -24,25 +24,37 @@ import com.progmasoft.tazel.label.TargetLabel import com.progmasoft.tazel.project.Module import com.progmasoft.tazel.project.ModuleLoader -/** What `tazel build` and `tazel run` were given on the command line. */ +/** What `tazel build`, `run`, `test`, `fmt` and `lint` were given on the command line. */ internal data class BuildOptions( val mode: BuildMode, val triple: String?, val selections: List<String>, - /** After `--`: what `tazel run` passes to the program. */ + /** After `--`: what `tazel run` passes to the program, or `fmt` and `lint` to their tools. */ val programArguments: List<String>, + /** `tazel fmt --check`: report what is not formatted instead of formatting it. */ + val check: Boolean = false, + /** `tazel lint --fix`: apply the fixes clang-tidy offers. */ + val fix: Boolean = false, ) /** Reads the options of `tazel build` and `tazel run`; [command] names it in messages. */ internal fun buildOptions(command: String, arguments: List<String>): BuildOptions { var mode = BuildMode.DEBUG var triple: String? = null + var check = false + var fix = false val selections = mutableListOf<String>() var index = 0 while (index < arguments.size) { when (val argument = arguments[index]) { - "--" -> return BuildOptions(mode, triple, selections, arguments.drop(index + 1)) + "--" -> return BuildOptions(mode, triple, selections, arguments.drop(index + 1), check, fix) "--release" -> mode = BuildMode.RELEASE + "--check" -> + if (command == "fmt") check = true + else throw Usage("tazel $command has no option --check; tazel fmt has") + "--fix" -> + if (command == "lint") fix = true + else throw Usage("tazel $command has no option --fix; tazel lint has") "--triple" -> { triple = arguments.getOrNull(index + 1) @@ -58,7 +70,7 @@ internal fun buildOptions(command: String, arguments: List<String>): BuildOption } index++ } - return BuildOptions(mode, triple, selections, emptyList()) + return BuildOptions(mode, triple, selections, emptyList(), check, fix) } /** A command line that is wrong; it exits with [ExitCode.USAGE]. */ @@ -69,6 +81,7 @@ internal enum class Purpose { BUILD, RUN, TEST, + FORMAT, } /** `tazel build`, `tazel run` and `tazel test`. */ @@ -159,13 +172,13 @@ internal class BuildCommands( return plan } - private data class Selected( + internal data class Selected( val module: Module, val labels: List<TargetLabel>, val triple: TargetTriple, ) - private fun select(options: BuildOptions, purpose: Purpose): Selected { + internal fun select(options: BuildOptions, purpose: Purpose): Selected { val root = loader.findRoot(environment.directory) val module = loader.load(root) val triple = options.triple?.let(TargetTriple::parse) ?: host @@ -187,7 +200,10 @@ internal class BuildCommands( throw Usage(error.message.orEmpty()) } val selected = module.select(selection) - val forTriple = selected.filter { triple in module.triplesOf(it, options.mode, host) } + // Formatting does not compile, so it takes the targets of every triple. + val forTriple = + if (purpose == Purpose.FORMAT) selected + else selected.filter { triple in module.triplesOf(it, options.mode, host) } if (selection is TargetLabel && forTriple.isEmpty()) { throw Stop("$selection is not built for $triple; name one of its triples with --triple") } diff --git a/sources/main/kotlin/com/progmasoft/tazel/cli/Main.kt b/sources/main/kotlin/com/progmasoft/tazel/cli/Main.kt index c9c4d61..4c963a6 100644 --- a/sources/main/kotlin/com/progmasoft/tazel/cli/Main.kt +++ b/sources/main/kotlin/com/progmasoft/tazel/cli/Main.kt @@ -45,6 +45,10 @@ internal val HELP = build builds targets, such as tazel build //app/:greeter or tazel build //... run builds and runs one bin target; what follows -- goes to the program test builds and runs test targets; what follows -- goes to every test + fmt formats the sources and headers of targets with clang-format and zig fmt; + --check reports instead of formatting + lint checks the C-family sources of targets with clang-tidy; --fix applies its fixes; + writes compile_commands.json custom runs a command of tazel_custom.txt, or lists them version prints the version of Tazel help prints this help @@ -58,7 +62,7 @@ internal val HELP = """ .trimIndent() -private val COMMANDS = listOf("build", "run", "test", "custom", "version", "help") +private val COMMANDS = listOf("build", "run", "test", "fmt", "lint", "custom", "version", "help") /** Where a command runs and what it starts programs with; tests give their own. */ internal class Environment( @@ -94,6 +98,14 @@ internal fun run( out.appendLine("tazel ${tazelVersion()}") ExitCode.SUCCESS } else usage(err, "tazel version takes no arguments") + "fmt", + "lint" -> + BuildCommands.guarded(err) { + val command = arguments.first() + val options = buildOptions(command, arguments.drop(1)) + val commands = SourceCommands(environment, out) + if (command == "fmt") commands.format(options) else commands.lint(options) + } "build", "run", "test" -> diff --git a/sources/main/kotlin/com/progmasoft/tazel/cli/SourceCommands.kt b/sources/main/kotlin/com/progmasoft/tazel/cli/SourceCommands.kt new file mode 100644 index 0000000..97b92de --- /dev/null +++ b/sources/main/kotlin/com/progmasoft/tazel/cli/SourceCommands.kt @@ -0,0 +1,86 @@ +/* + * SPDX-FileCopyrightText: 2026 Progmasoft <[email protected]> + * SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + */ + +package com.progmasoft.tazel.cli + +import com.progmasoft.tazel.driver.BuildPlan +import com.progmasoft.tazel.driver.CcToolchain +import com.progmasoft.tazel.driver.FileSets +import com.progmasoft.tazel.driver.Formatter +import com.progmasoft.tazel.driver.Linter +import com.progmasoft.tazel.driver.compileEntries +import com.progmasoft.tazel.driver.ownedFiles +import com.progmasoft.tazel.driver.writeCompileCommands +import com.progmasoft.tazel.dsl.Language +import com.progmasoft.tazel.dsl.Os +import com.progmasoft.tazel.dsl.TargetTriple +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardCopyOption + +/** `tazel fmt` and `tazel lint`: the tools that read sources without building them. */ +internal class SourceCommands(private val environment: Environment, private val out: Appendable) { + private val host = TargetTriple.host() + private val fileSets = FileSets(ignoreCase = host.os == Os.WINDOWS) + + /** + * Formats the files the selected targets own, on every triple they are built for, with + * clang-format and `zig fmt`; with `--check` it only reports the files that are not formatted. + */ + fun format(options: BuildOptions): Int { + val (module, labels, _) = BuildCommands(environment, out).select(options, Purpose.FORMAT) + val files = linkedSetOf<Path>() + for (label in labels) { + for (triple in module.triplesOf(label, options.mode, host)) { + val target = module.configure(label, triple, options.mode) + files += ownedFiles(module.declaration(label).first.directory, target.settings, fileSets) + } + } + val result = + Formatter(environment.programs, environment.processes, out) + .format(files.toList(), options.check, options.programArguments, module.root) + val verb = if (options.check) "Checked" else "Formatted" + out.appendLine( + "%12s %d file(s); %d run(s) failed".format(verb, result.filesChecked, result.failures) + ) + return if (result.failures == 0) ExitCode.SUCCESS else ExitCode.FAILURE + } + + /** + * Writes the compilation database of the selected targets and what they depend on, then checks + * the C-family sources of the selected targets with clang-tidy; with `--fix` it applies the fixes + * clang-tidy offers. + */ + fun lint(options: BuildOptions): Int { + val (module, labels, triple) = BuildCommands(environment, out).select(options, Purpose.BUILD) + val plan = BuildPlan(module, triple, options.mode) + labels.forEach(plan::add) + val entries = compileEntries(plan, CcToolchain(environment.programs, host), fileSets) + val database = writeCompileCommands(plan.outputRoot, entries) + // One place per module that does not change with the triple, for clangd (CompilationDatabase: + // .tazel) and other editors to be pointed at. + val shared = module.root.resolve(".tazel/compile_commands.json") + Files.copy(database, shared, StandardCopyOption.REPLACE_EXISTING) + for (target in plan.targets) { + if (target.label in labels && target.language == Language.ZIG) { + out.appendLine("%12s %s: Zig has no linter in Tazel".format("Skipped", target.label)) + } + } + val jobs = module.file.execution.jobs ?: Runtime.getRuntime().availableProcessors() + val result = + Linter(environment.programs, environment.processes, out, jobs) + .lint( + entries.filter { it.label in labels }, + plan.outputRoot, + options.fix, + options.programArguments, + ) + out.appendLine( + "%12s %d file(s); %d failed; compile_commands.json in %s" + .format("Linted", result.filesChecked, result.failures, shared.parent) + ) + return if (result.failures == 0) ExitCode.SUCCESS else ExitCode.FAILURE + } +} diff --git a/sources/main/kotlin/com/progmasoft/tazel/driver/SourceTools.kt b/sources/main/kotlin/com/progmasoft/tazel/driver/SourceTools.kt new file mode 100644 index 0000000..28c0190 --- /dev/null +++ b/sources/main/kotlin/com/progmasoft/tazel/driver/SourceTools.kt @@ -0,0 +1,236 @@ +/* + * SPDX-FileCopyrightText: 2026 Progmasoft <[email protected]> + * SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + */ + +package com.progmasoft.tazel.driver + +import com.progmasoft.tazel.dsl.Language +import com.progmasoft.tazel.dsl.ProjectException +import com.progmasoft.tazel.dsl.TargetSettings +import java.nio.file.Files +import java.nio.file.Path + +/** + * The extensions clang-format formats: the sources of the four C-family languages and their + * headers. + */ +private val CLANG_FORMAT_EXTENSIONS = + setOf( + "c", + "cc", + "cpp", + "cxx", + "c++", + "m", + "mm", + "h", + "hh", + "hpp", + "hxx", + "h++", + "inl", + "ipp", + "tpp", + ) + +private val HEADER_EXTENSIONS = setOf("h", "hh", "hpp", "hxx", "h++", "inl", "ipp", "tpp", "inc") + +/** + * How many files one run of a formatter gets, so that no command line grows past the limit of the + * system. + */ +private const val FILES_PER_RUN = 200 + +/** + * The files of a target that its formatter owns: its sources less its excludes, the headers below + * its header directories, and its textual headers. + */ +fun ownedFiles(directory: Path, settings: TargetSettings, fileSets: FileSets): List<Path> { + val files = linkedSetOf<Path>() + files += fileSets.expand(directory, settings.sources, settings.excludes) + for (headers in settings.headers + settings.privateHeaders) { + val start = directory.resolve(headers).normalize() + if (!Files.isDirectory(start)) continue + Files.walk(start).use { paths -> + paths + .filter { Files.isRegularFile(it) && extensionOf(it) in HEADER_EXTENSIONS } + .sorted() + .forEach(files::add) + } + } + files += fileSets.expand(directory, settings.textualHeaders) + return files.toList() +} + +private fun extensionOf(file: Path) = + file.fileName.toString().substringAfterLast('.', "").lowercase() + +/** What a formatter or linter run found. */ +data class ToolResult(val filesChecked: Int, val failures: Int) + +/** + * Formats files with the tools of their languages: clang-format for C, C++, Objective-C and + * Objective-C++ and their headers, `zig fmt` for Zig. Each tool finds its own configuration, such + * as the `.clang-format` above a file. + */ +class Formatter( + private val programs: Programs, + private val processes: Processes, + private val out: Appendable, +) { + /** Formats [files] in place, or with [check] only reports those that are not formatted. */ + fun format( + files: List<Path>, + check: Boolean, + extraArguments: List<String>, + directory: Path, + ): ToolResult { + val clang = files.filter { extensionOf(it) in CLANG_FORMAT_EXTENSIONS } + val zig = files.filter { extensionOf(it) == "zig" } + var failures = 0 + if (clang.isNotEmpty()) { + val tool = + programs.find("clang-format") ?: throw ProjectException("clang-format is not on PATH") + val flags = if (check) listOf("--dry-run", "--Werror") else listOf("-i") + for (chunk in clang.chunked(FILES_PER_RUN)) { + failures += + run( + listOf(tool.toString()) + flags + extraArguments + chunk.map(Path::toString), + directory, + ) + } + } + if (zig.isNotEmpty()) { + val tool = programs.find("zig") ?: throw ProjectException("zig is not on PATH") + val flags = if (check) listOf("fmt", "--check") else listOf("fmt") + for (chunk in zig.chunked(FILES_PER_RUN)) { + failures += + run( + listOf(tool.toString()) + flags + extraArguments + chunk.map(Path::toString), + directory, + ) + } + } + return ToolResult(clang.size + zig.size, failures) + } + + private fun run(command: List<String>, directory: Path): Int { + val result = processes.runGathered(command, directory) + if (result.output.isNotBlank()) + synchronized(out) { out.append(result.output.trimEnd()).appendLine() } + return if (result.exitCode == 0) 0 else 1 + } +} + +/** One entry of a compilation database, as clangd and clang-tidy read it. */ +data class CompileEntry( + val label: com.progmasoft.tazel.label.TargetLabel, + val directory: Path, + val file: Path, + val arguments: List<String>, + val output: Path, +) + +/** + * The compilation database of a plan: one entry for each translation unit of its C-family targets, + * with the command `tazel build` would run, less its depfile options. + */ +fun compileEntries( + plan: BuildPlan, + toolchain: CcToolchain, + fileSets: FileSets, +): List<CompileEntry> { + val entries = mutableListOf<CompileEntry>() + for (target in plan.targets) { + if (target.language == Language.ZIG) continue + val settings = target.target.settings + val commands = + CcCommands(plan, toolchain.compiler(settings.compiler, target.language, plan.triple)) + commands.stageHeaders(target) + val sources = + commands.translationUnits( + target, + fileSets.expand(target.packageDirectory, settings.sources, settings.excludes), + ) + for (source in sources) { + val objectFile = commands.objectOf(target, source) + val depfile = objectFile.resolveSibling(objectFile.fileName.toString() + ".d") + val command = commands.compile(target, source, objectFile, depfile).toMutableList() + val md = command.indexOf("-MD") + if (md >= 0 && command.getOrNull(md + 1) == "-MF") repeat(3) { command.removeAt(md) } + entries += CompileEntry(target.label, target.packageDirectory, source, command, objectFile) + } + } + return entries +} + +/** Writes [entries] as `compile_commands.json` into [directory] and returns the file. */ +fun writeCompileCommands(directory: Path, entries: List<CompileEntry>): Path { + val text = + entries.joinToString(",\n", prefix = "[\n", postfix = "\n]\n") { entry -> + " {\"directory\": ${json(entry.directory.toString())}, \"file\": ${json(entry.file.toString())}, " + + "\"output\": ${json(entry.output.toString())}, \"arguments\": [" + + entry.arguments.joinToString(", ") { json(it) } + + "]}" + } + Files.createDirectories(directory) + val file = directory.resolve("compile_commands.json") + Files.writeString(file, text) + return file +} + +private fun json(text: String): String { + val escaped = StringBuilder("\"") + for (character in text) { + when { + character == '"' -> escaped.append("\\\"") + character == '\\' -> escaped.append("\\\\") + character == '\n' -> escaped.append("\\n") + character == '\r' -> escaped.append("\\r") + character == '\t' -> escaped.append("\\t") + character < ' ' -> escaped.append("\\u%04x".format(character.code)) + else -> escaped.append(character) + } + } + return escaped.append('"').toString() +} + +/** + * Runs clang-tidy on translation units, with the compilation database in [database], [jobs] at a + * time. + */ +class Linter( + private val programs: Programs, + private val processes: Processes, + private val out: Appendable, + private val jobs: Int, +) { + fun lint( + entries: List<CompileEntry>, + database: Path, + fix: Boolean, + extraArguments: List<String>, + ): ToolResult { + if (entries.isEmpty()) return ToolResult(0, 0) + val tool = programs.find("clang-tidy") ?: throw ProjectException("clang-tidy is not on PATH") + // Fixes to one header from two translation units at once would overwrite each other. + val parallel = if (fix) 1 else jobs + val failures = + JobPool(parallel, keepGoing = true).map(entries, { "lint ${it.file}" }) { entry -> + val command = + listOf(tool.toString(), "-p", database.toString(), "--quiet") + + (if (fix) listOf("--fix") else emptyList()) + + extraArguments + + entry.file.toString() + val result = processes.runGathered(command, entry.directory) + val output = + result.output.lines().filterNot { it.matches(Regex("""^\d+ warnings? generated\.$""")) } + if (output.any(String::isNotBlank)) { + synchronized(out) { out.append(output.joinToString("\n").trimEnd()).appendLine() } + } + if (result.exitCode == 0) 0 else 1 + } + return ToolResult(entries.size, failures.sum()) + } +} diff --git a/sources/test/kotlin/com/progmasoft/tazel/cli/SourceCommandTest.kt b/sources/test/kotlin/com/progmasoft/tazel/cli/SourceCommandTest.kt new file mode 100644 index 0000000..6950c64 --- /dev/null +++ b/sources/test/kotlin/com/progmasoft/tazel/cli/SourceCommandTest.kt @@ -0,0 +1,134 @@ +/* + * SPDX-FileCopyrightText: 2026 Progmasoft <[email protected]> + * SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + */ + +package com.progmasoft.tazel.cli + +import com.progmasoft.tazel.driver.Programs +import com.progmasoft.tazel.dsl.TargetTriple +import java.nio.file.Files +import java.nio.file.Path +import kotlin.test.Test +import kotlin.test.assertContains +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** + * Formats and lints a small module with the clang-format and clang-tidy of this machine. Without + * them the tests return early. + */ +class SourceCommandTest { + private val os = TargetTriple.host().os + private val programs = Programs(os) + + private fun module(): Path { + val root = Files.createTempDirectory("tazel-source-").toRealPath() + fun write(path: String, text: String) { + val file = root.resolve(path) + Files.createDirectories(file.parent) + Files.writeString(file, text.trimIndent() + "\n") + } + write("MODULE.kts", """module { name = "sample" }""") + write(".clang-format", "BasedOnStyle: LLVM") + write( + "core/BUILD.kts", + """ + isPublic = true + c.staticlib("core") { + sources(glob("src/*.c")) + headers("include") + stripIncludePrefix = "include" + includePrefix = "sample" + } + """, + ) + write("core/include/core.h", "int sample_add(int a, int b);") + write( + "core/src/core.c", + "#include <sample/core.h>\nint sample_add(int a, int b) { return a + b; }", + ) + return root + } + + private fun tazel(root: Path, vararg arguments: String): Triple<Int, String, String> { + val out = StringBuilder() + val err = StringBuilder() + val code = run(arguments.toList(), out, err, Environment(directory = root)) + return Triple(code, out.toString(), err.toString()) + } + + @Test + fun fmtChecksThenFormats() { + if (programs.find("clang-format") == null) return + val root = module() + try { + val header = root.resolve("core/include/core.h") + Files.writeString(header, "int sample_add( int a,int b );\n") + val (failed, _, _) = tazel(root, "fmt", "--check", "//...") + assertEquals(ExitCode.FAILURE, failed) + assertEquals("int sample_add( int a,int b );\n", Files.readString(header)) + + val (formatted, out, err) = tazel(root, "fmt", "//...") + assertEquals(ExitCode.SUCCESS, formatted, out + err) + assertEquals("int sample_add(int a, int b);\n", Files.readString(header)) + + val (checked, checkOut, checkErr) = tazel(root, "fmt", "--check", "//...") + assertEquals(ExitCode.SUCCESS, checked, checkOut + checkErr) + assertContains(checkOut, "Checked 2 file(s)") + } finally { + root.toFile().deleteRecursively() + } + } + + @Test + fun lintWritesTheDatabaseAndAppliesFixes() { + if (programs.find("clang-tidy") == null) return + if (programs.find("clang") == null && programs.find("gcc") == null) return + val root = module() + try { + Files.writeString( + root.resolve(".clang-tidy"), + "Checks: '-*,readability-braces-around-statements'\nWarningsAsErrors: '*'\n", + ) + val source = root.resolve("core/src/core.c") + Files.writeString( + source, + "#include <sample/core.h>\n" + + "int sample_add(int a, int b) {\n if (a < 0)\n return b;\n return a + b;\n}\n", + ) + val (failed, out, err) = tazel(root, "lint", "//...") + assertEquals(ExitCode.FAILURE, failed, out + err) + assertContains(out, "readability-braces-around-statements") + + val database = root.resolve(".tazel/compile_commands.json") + assertTrue(Files.isRegularFile(database)) + val text = Files.readString(database) + assertContains(text, source.toString().replace("\\", "\\\\")) + assertTrue("-MF" !in text, text) + + // clang-tidy fails a run that found errors, also when it fixed them; the next run is clean. + val (fixed, fixOut, fixErr) = tazel(root, "lint", "--fix", "//...") + assertEquals(ExitCode.FAILURE, fixed, fixOut + fixErr) + assertContains(fixOut, "applied 2 of 2 suggested fixes") + assertContains(Files.readString(source), "if (a < 0) {") + + val (clean, cleanOut, cleanErr) = tazel(root, "lint", "//...") + assertEquals(ExitCode.SUCCESS, clean, cleanOut + cleanErr) + } finally { + root.toFile().deleteRecursively() + } + } + + @Test + fun eachFlagBelongsToItsCommand() { + val root = module() + try { + assertEquals(ExitCode.USAGE, tazel(root, "lint", "--check").first) + assertEquals(ExitCode.USAGE, tazel(root, "fmt", "--fix").first) + assertEquals(ExitCode.USAGE, tazel(root, "build", "--fix").first) + } finally { + root.toFile().deleteRecursively() + } + } +} |