Sybren A. Stüvel 3f4a9025fe Manager tests: replace assert.NoError() with require.NoError()
Back in the days when I wrote the code, I didn't know about the
`require` package yet. Using `require.NoError()` makes the test code
more straight-forward.

No functional changes, except that when tests fail, they now fail
without panicking.
2024-03-16 11:09:18 +01:00

76 lines
1.8 KiB
Go

package job_compilers
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"os"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadScriptsFrom_skip_nonjs(t *testing.T) {
thisDirFS := os.DirFS(".")
compilers, err := loadScriptsFrom(thisDirFS)
require.NoError(t, err, "input without JS files should not cause errors")
assert.Empty(t, compilers)
}
func TestLoadScriptsFrom_on_disk_js(t *testing.T) {
scriptsFS := os.DirFS("scripts-for-unittest")
compilers, err := loadScriptsFrom(scriptsFS)
require.NoError(t, err)
expectKeys := map[string]bool{
"echo-and-sleep": true,
"simple-blender-render": true,
// Should NOT contain an entry for 'empty.js'.
}
assert.Equal(t, expectKeys, keys(compilers))
}
func TestLoadScriptsFrom_embedded(t *testing.T) {
initEmbeddedFS()
compilers, err := loadScriptsFrom(embeddedScriptsFS)
require.NoError(t, err)
expectKeys := map[string]bool{
"echo-sleep-test": true,
"simple-blender-render": true,
}
assert.Equal(t, expectKeys, keys(compilers))
}
func BenchmarkLoadScripts_fromEmbedded(b *testing.B) {
zerolog.SetGlobalLevel(zerolog.Disabled)
initEmbeddedFS()
for i := 0; i < b.N; i++ {
compilers, err := loadScriptsFrom(embeddedScriptsFS)
require.NoError(b, err)
assert.NotEmpty(b, compilers)
}
}
func BenchmarkLoadScripts_fromDisk(b *testing.B) {
zerolog.SetGlobalLevel(zerolog.Disabled)
onDiskFS := os.DirFS("scripts-for-unittest")
for i := 0; i < b.N; i++ {
compilers, err := loadScriptsFrom(onDiskFS)
require.NoError(b, err)
assert.NotEmpty(b, compilers)
}
}
// keys returns the set of keys of the mapping.
func keys[K comparable, V any](mapping map[K]V) map[K]bool {
keys := map[K]bool{}
for k := range mapping {
keys[k] = true
}
return keys
}