tests(bats): minor improvement to test generation.

This commit is contained in:
Alexandre Pujol 2024-10-21 14:13:25 +01:00
parent 7e09351f8f
commit d6d4648106
Failed to generate hash of commit
2 changed files with 19 additions and 10 deletions

View file

@ -40,7 +40,7 @@ type Config struct {
TldrDir *paths.Path // Default: tests/tldr
TldrFile *paths.Path // Default: tests/tldr.yml
TestsFile *paths.Path // Default: tests/tests.yml
BatsDir *paths.Path // Default: tests/bats
BatsDir *paths.Path // Default: tests/bats_dirty
}
func NewConfig() *Config {
@ -70,6 +70,12 @@ func run() error {
}
tests = tests.Filter()
if err := cfg.BatsDir.RemoveAll(); err != nil {
return err
}
if err := cfg.BatsDir.MkdirAll(); err != nil {
return err
}
for _, test := range tests {
if err := test.Write(cfg.BatsDir); err != nil {
return err

View file

@ -18,11 +18,18 @@ const tmplTest = `#!/usr/bin/env bats
# apparmor.d - Full set of apparmor profiles
# Copyright (C) 2024 Alexandre Pujol <alexandre@pujol.io>
# SPDX-License-Identifier: GPL-2.0-only
load common
setup_file() {
aa_setup
}
{{ $name := .Name -}}
{{ range .Commands }}
# bats test_tags={{ $name }}
@test "{{ $name }}: {{ .Description }}" {
{{ .Cmd }}
aa_check
}
{{ end }}
`
@ -56,20 +63,13 @@ type Command struct {
Cmd string
}
func NewTest() *Test {
return &Test{
Name: "",
Commands: []Command{},
}
}
// HasProfile returns true if the program in the scenario is profiled in apparmor.d
func (t *Test) HasProfile() bool {
func (t Test) HasProfile() bool {
return slices.Contains(Profiles, t.Name)
}
// IsInstalled returns true if the program in the scenario is installed on the system
func (t *Test) IsInstalled() bool {
func (t Test) IsInstalled() bool {
if _, err := exec.LookPath(t.Name); err != nil {
return false
}
@ -82,6 +82,9 @@ func (t Test) Write(dir *paths.Path) error {
}
path := dir.Join(t.Name + ".bats")
if paths.New("tests/bats").Join(t.Name + ".bats").Exist() {
path = dir.Join("00." + t.Name + ".bats")
}
content := renderBatsFile(t)
if err := path.WriteFile([]byte(content)); err != nil {
return err