Giter VIP home page Giter VIP logo

leia's Introduction

Leia

Leia is a testing utility that tests code blocks in documentation. This makes tests easy to write and also ensures documentation is up to date and working. Behind the scenes documentation is parsed and run as a series of mocha tests.

Leia will

  • Consolidate code examples and tests into a single, easy to understand and write markdown file
  • Write functional tests quickly in an accessible and lowest common denominator language (eg sh/bash/dash etc)
  • Pass on exit status code 0, fail on anything else
  • Work cross platform-ish, with some caveats, see Shell Considerations below
  • Keep Lando honest so he can be a real hero who doesn't betray his friends again

Installation

# With npm
npm install @lando/leia

Basics

A very basic example of a valid Leia test is below. It must have a single H1 header, at least one H2 header and then a code block where the comment is the human readable test description and the command below is the test.

# Some Example

## Testing

# A description of my test
the command i am running

Usage

You can invoke leia as a command line tool or directly require it in a module.

CLI

npx leia

Cleverly converts markdown files into mocha cli tests

USAGE
  $ leia <files> <patterns> [options]

ARGUMENTS
  TESTS  files or patterns to scan for test

OPTIONS
  -c, --cleanup-header=cleanup-header  [default: Clean,Tear,Burn] sections that start with these headers are cleanup commands
  -h, --help                           show CLI help
  -i, --ignore=ignore                  files or patterns to ignore
  -r, --retry=retry                    [default: 1] the amount of retries a failing test should get
  -s, --setup-header=setup-header      [default: Start,Setup,This is the dawning] sections that start with these headers are setup commands
  -t, --test-header=test-header        [default: Test,Validat,Verif] sections that start with these headers are tests
  -v, --version                        show CLI version
  --debug                              show debug output
  --shell=shell                        the shell to use for the tests, default is autodetected
  --stdin                              attach stdin when the test is run

EXAMPLES
  leia README.md
  leia README.md "examples/**/*.md" --retry 6 --test-header Tizzestin
  leia "examples/*.md" --ignore BUTNOTYOU.md test --stdin
  leia README.md --shell cmd

Module

# Instantiate a new leia
const Leia = require('@lando/leia');
const leia = new Leia();

// Find some tests
const files = leia.find(['examples/**.md']);
// Parse those files into leia test metadata
const sources = leia.parse(files);
// Generate the mocha tests
const tests = leia.generate(sources);
// Run the tests
const runner = leia.run(tests);
runner.run((failures) => process.exitCode = failures ? 1 : 0);

For more details on specific options check out the code docs

Markdown Syntax

In order for your markdown file to be recognized as containing functional tests it needs to have at least the following

1. A h1 Header

# Something to identify these tests

2. A h2 Header

By default our parser will look for a section that beings with the word "Testing". This section will contain your tests.

## Testing

You can customize the word(s) that leia will look for to identify the testing section(s) using the --test-header option. You can also run npm leia --help to get a list of default words.

3. A code block with at least one command and comment

Under the above h2 sections you need to have a triple tick markdown code block that contains at least one comment and one command. The comment will be the human readable description of what the test does.

Here is a basic code block that runs one test

# Should cat a file
cat test.txt

If you want to learn more about the syntax and how leia puts together the above, check out this example

Environment Variables

leia will also set the following environment variables for each test that is running so you can use them for stuff.

Here are the values you would expect for the Should set envvars with the test number test in examples/environment.md running on Leia version v1.0.0 with --retry=1.

# generic vars
LEIA=true
LEIA_ENVIRONMENT=true
LEIA_VERSION=1.0.0

# test vars
LEIA_TEST_RUNNING=true
LEIA_TEST_ID=environment
LEIA_TEST_NUMBER=4
LEIA_TEST_RETRY=1
LEIA_TEST_STAGE=test

Note: LEIA_TEST_STAGE can be either setup, test or cleanup and LEIA_TEST_NUMBER resets to 1 for each LEIA_TEST_STAGE.

Shell considerations

leia will autodetect your shell and use a bashy one if available.

  • On POSIX systems it will prefer bash or zsh if available with a fallback to sh.
  • On Windows systems it will prefer bash if available with a fallback to cmd.

You can also explicitly tell leia what shell to use with the --shell option. However, currently only bash, sh, zsh, cmd, powershell and pwsh are supported options.

In most use cases it's best to just let leia decide the shell to use automatically.

Advanced Usage

Leia also allows you to specify additional h2 sections in your markdown for setup and cleanup commands that run before and after your core tests. You can tell leia what words these headers should start with in order to be flagged as setup and cleanup commands using the --setup-header and --cleanup-header options.

Here is an example of a markdown file with Setup, Testing and Cleanup sections. And here is a whole directory of examples that we test on every commit.

Issues, Questions and Support

If you have a question or would like some community support we recommend you join us on Slack. Note that this is the Slack community for Lando but we are more than happy to help with this module as well!

If you'd like to report a bug or submit a feature request then please use the issue queue in this repo.

Changelog

We try to log all changes big and small in both THE CHANGELOG and the release notes.

Development

git clone https://github.com/lando/leia.git && cd leia
npm install

If you dont' want to install Node 18 for whatever reason you can install Lando and use that:

git clone https://github.com/lando/leia.git && cd leia
# Install deps and get node
lando start

# Run commands
lando node
lando npm install
lando npx leia

Testing

# Lint the code
npm run lint

# Run unit tests
npm run test:unit

Releasing

To deploy and publish a new version of the package to the npm registry you need only create a release on GitHub with a semver tag.

Note that prereleases will get pushed to the edge tag on the npm registry.

Maintainers

Contributors

Made with contributors-img.

Legacy Version

You can still install the older version of Leia eg leia-parser.

npm install leia-parser

And its documentation lives on here.

Other Resources

leia's People

Contributors

codebymikey avatar dependabot[bot] avatar github-actions[bot] avatar pirog avatar reynoldsalec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

leia's Issues

Migrate to GitHub actions

We need to remove our reliance on Travis and sell out harder to our Microsoft overlords.

The DevOps we need is:

On Pull Requests

We need three different workflows:

  1. Runs on just ubuntu and runs the lint task
  2. Runs on Windows, MacOS and Linux and runs the unit tests
  3. Runs on Windows, MacOS and Linux and runs the leia functional tests

On Tags

  1. We need a workflow that deploys to npm
  2. We should rename the project from lando-parser to @lando/leia.

You can look at lando/lando, lando/cli or lando/hyperdrive for inspiration on how to do the above devops.

How to handle lots of quote recursion in an execute string...

We're using Leia - because it's awesome - to do some testing, and have hit a stumbling block around nesting ' and "...

If I have the line

docker-compose exec -T php sh -c "sed -i \'1 aremove_filter('template_redirect','redirect_canonical');' /app/web/content/themes/twentytwenty/functions.php"

because I want the second line of my functions.php file to be remove_filter('template_redirect','redirect_canonical');

How could I go about quoting it, so that mocha doesn't freak out...

/home/tobybellwood/sites/wordpress-example/test/docker-compose-wordpress-simple-php-7-4-nginx-mariadb.func.js:225
    cli.exec('docker-compose exec -T php sh -c "sed -i '1 aremove_filter('template_redirect','redirect_canonical');' /app/web/content/themes/twentytwenty/functions.php"').then(res => {
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

adding a \ before the ' works for mocha but doesn't do anything

    cli.exec('docker-compose exec -T php sh -c "sed -i \'1 aremove_filter(\'template_redirect\',\'redirect_canonical\');\' /app/web/content/themes/twentytwenty/functions.php"').then(res => {

If there's no easy fix, I can just find a way that doesn't involve single quotes ๐Ÿ˜ฑ

Leia does not handle `\` very well eg at all

In some cases its desirable for your README to contain the more readable

lando init \
  --source github \
  --recipe mean \
  --github-auth "$MY_GITHUB_TOKEN" \
  --github-repo [email protected]:lando/lando.git \
  --name my-awesome-app

However, leia seems to very much not like \. I'm guessing this is because leia reads each line as a separate command and thus joins them with &&. Maybe there is a way for leia to concatenate lines ending in \ before being sent downstream where they are joined by &&

Tests not generated correctly on Windows

Not sure if this is universal or an edge case but while setting up GitHub action Leia tests on Windows 2019 for Hyperdrive the test files are not generated correctly. You should be able to replicate with:

git clone https://github.com/lando/hyperdrive.git
cd hyperdrive
yarn
yarn generate:leia-tests -vvv

After the above you should have a Leia test at test/leia/basics-example.func.js but you do not. Based on the output it looks like it detects the corresponding README correctly but it fails validation.

Leia will also scans through dep dirs like `vendor` and `node_modules`

This should also very well be a bug or documentation update

Is your feature request related to a problem? Please describe.
Leia will scan large directories specified in loose globz like node_modules and sometimes there are files that get flagged as false positives. This results in the generation of some tests that definitely shouldnt be tests.

Describe the solution you'd like
Not exactly sure but here are things to consider

  1. It might already be possible to specify glob excludes with additional -p patterns based directly to leia. If this is true then we probably just want to update docs and spin up a tests
  2. Alternatively we might want to consider some sort of --exclude option
  3. Alternatively we could just automatically exclude common dependency directories like node_modules or vendor

kabob case in name doesnt really work

eg

  l-337-example
    1) should start successfully
    โœ“ should run api 3 and 4 services together in peace and security (5044ms)
    โœ“ should destroy successfully (5190ms)

and should be

  l337-example
    1) should start successfully
    โœ“ should run api 3 and 4 services together in peace and security (5044ms)
    โœ“ should destroy successfully (5190ms)

change the default extension

we should change the default file extension from .func.js to .leia.js. This will help avoid collisions with more generic func tests that are similarly named and also make it more explicit about what kinds of tests these are.

escape escape characters

We should have better handling around escape characters. Example from the postgres example in Lando

lando psql -U postgres database -c "\dt"
lando psql -U postgres database -c "\dt"

On the leia side \dt needs to be escaped as \\dt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.