dream_snippets

Checks that every code block in the docs of a Gleam project is proven: each Gleam block matches a tested snippet file, and each other block is proven in the way that fits its kind (see dream_snippets/check).

Run it from the project root:

Run the check Tested snippet

gleam run -m dream_snippets

It prints one line for each problem, as file:line: reason, sorted by file and line, then a summary line, for example 12 blocks and 0 diff blocks checked in 9 files, 0 problems; 1 file block, 3 command blocks and 4 output blocks checked; 0 illustration blocks. It stops with exit code 1 when it finds a problem or cannot run. It never uses the network: a badge link is changed to a local path and the file is read from the working tree. For a diff block it runs git diff in the project root, and for code at an older release it runs git ls-tree and git show there.

The rules are in dream_snippets/check. The settings, read from the gleam.toml of the project, are in dream_snippets/config.

Types

An error that stops the check before it can report.

pub type CheckError {
  InvalidConfig(error: config.ConfigError)
  ListFailed(path: String, reason: simplifile.FileError)
  ReadFailed(path: String, reason: simplifile.FileError)
  MissingRoot(path: String)
}

Constructors

  • InvalidConfig(error: config.ConfigError)

    gleam.toml cannot be used.

  • ListFailed(path: String, reason: simplifile.FileError)

    A path could not be looked at, or a directory could not be listed.

  • ReadFailed(path: String, reason: simplifile.FileError)

    A document could not be read.

  • MissingRoot(path: String)

    A root that [tools.dream_snippets] sets does not exist.

What the command prints, and the status it stops with.

pub type Output {
  Output(
    standard_output: List(String),
    standard_error: List(String),
    exit_status: Int,
  )
}

Constructors

  • Output(
      standard_output: List(String),
      standard_error: List(String),
      exit_status: Int,
    )

The result of checking a project: the project, the report of all its documents, and the number of files that were checked.

pub type ProjectReport {
  ProjectReport(
    project: check.Project,
    report: check.Report,
    file_count: Int,
  )
}

Constructors

Values

pub fn check_project(
  directory: String,
) -> Result(ProjectReport, CheckError)

Checks every document of a project.

It reads the configuration from gleam.toml (see config.parse), lists the files of the checked roots, and checks each one. Snippet files are read from the working tree, and git runs in directory for the diff blocks.

Parameters

  • directory: the project root, for example "."

Returns

The project, the merged report, and the number of files checked. The paths in the problems are relative to directory.

Examples

Check a project and print its summary Tested snippet

import dream_snippets

pub fn summary_of(directory: String) -> List(String) {
  let output = dream_snippets.output(dream_snippets.check_project(directory))
  output.standard_output
}

Errors

InvalidConfig when gleam.toml cannot be used, MissingRoot when a configured root does not exist, and ListFailed or ReadFailed when a path cannot be listed or read.

pub fn describe_error(error: CheckError) -> String

Describes an error that stops the check, in one line (see check.one_line).

Examples

Describe an error that stops the check Tested snippet

import dream_snippets.{MissingRoot}

pub fn missing_root_message() -> String {
  // Returns "examples is set in [tools.dream_snippets] in gleam.toml, but it does not exist"
  dream_snippets.describe_error(MissingRoot("examples"))
}
pub fn main() -> Nil

Checks the docs of the project in the current directory, prints each problem and a summary, and stops with exit code 1 if there is a problem or an error, so that make targets, hooks and CI fail too.

Examples

Run the check from a module of your project Tested snippet

import dream_snippets

pub fn main() -> Nil {
  // Prints each problem and the summary line, and stops with exit status 1
  // when there is a problem.
  dream_snippets.main()
}
pub fn output(
  result: Result(ProjectReport, CheckError),
) -> Output

Turns the result of check_project into what the command prints and its exit status.

For a report, the standard output is one line for each problem (see check.describe_problem), sorted by file and line, then the summary line (see check.summary). The exit status is 0 with no problems and 1 otherwise. For an error, the standard error is one line, error: <description>, and the exit status is 1.

Examples

Turn an error into the command output Tested snippet

import dream_snippets.{type Output, MissingRoot}

pub fn output_of_missing_root() -> Output {
  // Returns Output(
  //   standard_output: [],
  //   standard_error: [
  //     "error: docs is set in [tools.dream_snippets] in gleam.toml, but it does not exist",
  //   ],
  //   exit_status: 1,
  // )
  dream_snippets.output(Error(MissingRoot("docs")))
}
✨ Search Document