Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: add bats support #600

Closed
kolyshkin opened this issue Aug 19, 2020 · 9 comments
Closed

syntax: add bats support #600

kolyshkin opened this issue Aug 19, 2020 · 9 comments

Comments

@kolyshkin
Copy link

Bats (https://github.com/bats-core/bats-core/) is a tool to write scripts in bash, with a few twists. A .bats file looks very much like a shell file, except for some added functions and features.

I think, the only change that is absolutely required to support bats is to recognise @test statement.

@test "some name" { denotes a test case. From the purpose of a formatter, I think it can be treated similar to function some_name {.

For the reference, shellcheck has added .bats support here: damienrg/shellcheck@f187382

@mvdan
Copy link
Owner

mvdan commented Aug 22, 2020

It's unfortunate that Bats isn't compatible with POSIX Shell; I imagine they could have done something similar to what Go did, like:

function TestFooBar {
    [...]
}

But in any case, that ship has sailed. They created their own language, and I clearly can't resist that or the tool's popularity.

At least they stuck to Bash and only made that one language change. I think we could add another shell language variant based off of Bash, and teach the parser about an extra reserved word.

@mvdan mvdan changed the title Add bats support syntax: add bats support Aug 22, 2020
@mvdan mvdan added this to the 3.2.0 milestone Aug 22, 2020
@mvdan
Copy link
Owner

mvdan commented Aug 30, 2020

Annoyingly enough, upstream bats allows this:

@test foo bar { body; }

@test "foo bar" "baz" { body; }

That is, it supports multiple words as the test description, not just one. I don't think anyone actually does that, and they don't have a formal language specification other than the somewhat horrifying regular expression below, so I'll go ahead and do the same thing shellcheck does - only support the @test WORD { COMMANDS; } form.

export BATS_TEST_PATTERN="^[[:blank:]]*@test[[:blank:]]+(.*[^[:blank:]])[[:blank:]]+\{(.*)\$"

@kolyshkin
Copy link
Author

In addition to the above comment, bats convert those test names to valid bash function names, e.g.

  • @test foo bar { becomes test_foo_bar() {
  • @test "foo bar" "baz" { -> test_foo_bar-22_-22baz() {

The internal test name is available at ${BATS_TEST_NAME}.

The test name encoding is done here: https://github.com/bats-core/bats-core/blob/master/libexec/bats-core/bats-preprocess#L4

@mvdan
Copy link
Owner

mvdan commented Sep 17, 2020

I imagine that doesn't really concern us, because we never have to build the test name. I still find it bizarre that they allow multiple words in that syntax.

@mvdan
Copy link
Owner

mvdan commented Sep 29, 2020

This is now in master; please test it and let me know how it works. Note that using something like shfmt -w . to walk directories will not find *.bats files just yet; that's a bit that I'm still working on.

@kolyshkin
Copy link
Author

@mvdan thanks so much for implementing this!

I just compiled shfmt from the current HEAD (which happens to be c5ff78f) and it shows nothing for bats files.

For example

$ cat << EOF > a.bats
> @test "one two" {
>   echo  foo
>  }
> EOF
$ ./shfmt -ln bats -d a.bats; echo $? 
0
$ ./shfmt -ln bats a.bats; echo $? 
0

Just in case, I checked the same binary works with bash input. Not sure if I'm missing something...

@mvdan
Copy link
Owner

mvdan commented Sep 30, 2020

Oh dear, this is why unit tests aren't always enough :)

@mvdan mvdan reopened this Sep 30, 2020
@mvdan mvdan closed this as completed in 08ecaf9 Sep 30, 2020
@mvdan
Copy link
Owner

mvdan commented Sep 30, 2020

Okay, that was a silly regression introduced in a "cleanup" done around the same time, but unrelated to Bats support. I've added copious tests, so that won't happen again. Please try again.

@kolyshkin
Copy link
Author

aaand it works now, thanks! 🤗

kolyshkin added a commit to kolyshkin/cri-o that referenced this issue Oct 12, 2020
Most *.bats tests here use tabs; let's reformat this file, too.

Brought to you by

	shfmt -ln bats -w status.bats

(yes, shfmt added bats support recently, see
mvdan/sh#600).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/runc that referenced this issue Oct 12, 2020
Various bats tests use various types of indentation (about half
is tabs, the rest is 2 spaces, 4 spaces, etc.).

Let's bring it to one style (tabs) using recently added
shfmt support for bats files (see [1]).

This commit is brought to you by

	shfmt -ln bats -w tests/integration/*.bats

[1] mvdan/sh#600

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/cri-o that referenced this issue Oct 12, 2020
Most *.bats tests here use tabs; let's reformat this file, too.

Brought to you by

	shfmt -ln bats -w status.bats

(yes, shfmt added bats support recently, see
mvdan/sh#600).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/cri-o that referenced this issue Oct 12, 2020
Most *.bats tests here use tabs; let's reformat this file, too.

Brought to you by

	shfmt -ln bats -w status.bats

(yes, shfmt added bats support recently, see
mvdan/sh#600).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/cri-o that referenced this issue Oct 13, 2020
shfmt has recently added support for bats files formatting
(see mvdan/sh#600), so let's use it.
We have to use git HEAD for now since there was no release done
with this feature yet.

Unfortunately, shfmt introduces an alledged regression (see [1])
so we have to specify `-ln bash` explicitly in Makefile.

This commit has brought to you by

	go get 'mvdan.cc/sh/v3@master'
	go mod vendor
	go mod tidy

[1] cri-o#4253

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
kolyshkin added a commit to kolyshkin/cri-o that referenced this issue Oct 13, 2020
shfmt has recently added support for bats files formatting (see [1]),
so let's use it.  We have to use git HEAD for now since there was no
release done with this feature yet.

Unfortunately, shfmt introduces an alledged regression (see [2])
so we have to specify `-ln bash` explicitly in Makefile.

This commit has (mostly) brought to you by

	go get 'mvdan.cc/sh/v3@master'
	go mod vendor
	go mod tidy

[1] mvdan/sh#600
[2] mvdan/sh#617

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
dqminh pushed a commit to dqminh/runc that referenced this issue Feb 3, 2021
Various bats tests use various types of indentation (about half
is tabs, the rest is 2 spaces, 4 spaces, etc.).

Let's bring it to one style (tabs) using recently added
shfmt support for bats files (see [1]).

This commit is brought to you by

	shfmt -ln bats -w tests/integration/*.bats

[1] mvdan/sh#600

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants