charmbracelet / vhs

Your CLI home video recorder đŸ“¼
MIT License
14.37k stars 240 forks source link

Make VHS available as a library to be used in test suites and make possible programmatically detect golden file changes #462

Open rija opened 3 months ago

rija commented 3 months ago

Is your feature request related to a problem? Please describe.

Hello, I intend to write regular go tests on CLI projects and we use GitLab for CI/CD (so VHS Github action won't help).

Describe the solution you'd like

I imagine something like below (I'm beginner at go so I apologise if it's not idiomatic, I'm quite open about how it could be done)

import (
    "github.com/charmbracelet/vhs"
    "testing"
)
...

func TestIntegrate(t *testing.T) {
    diff, err := vhs.PlayAndCompare("app.tape", "expected_behaviours.txt")
    if err != nil {
        t.Error(err.Error())
    }
    if diff != nil {
        t.Error(diff)
    }
}

Where app.tape will start with

Output a_special_name_the_library_can_find_automatically.txt
Hide
Type "go build -o hello . && clear"
Enter
Show
Type ./hello
Sleep 1s
...

Or instead of Output a tape directive that output the golden file only could be used (e.g: OutputGolden) or the library does it automatically internally

Describe alternatives you've considered

My current workaround is to execute the VHS cli (with a tape that generate a golden file) from the go test code and ReadFile the golden file before and after the call to VHS, then test whether the two files' content are the same. It feels clumsy (messes up the git status) and comparing the two versions of golden files make the code verbose if to be done efficiently

Additional context Add any other context or screenshots about the feature request here.

maaslalani commented 1 month ago

Hey! You might want to check out https://github.com/charmbracelet/vhs-action if you're using GitHub btw.