Skip to content

Commit

Permalink
cmd/cue/cmd: add a Go test covering the exposed API
Browse files Browse the repository at this point in the history
We have at least three users who got broken by a recent change of mine
to how the exposed cmd.New API behaves. Ahead of fixing that breakage,
add tests which mimic the reasonable uses of the public API
which I have seen from these users.

For #3458.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I80fa81c3f3d700fc575f3b337abae11ae55ea8de
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202645
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Oct 17, 2024
1 parent b010875 commit 1f8fb57
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cmd/cue/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd_test

import (
"bytes"
"context"
"io"
"strings"
"testing"

"cuelang.org/go/cmd/cue/cmd"
"github.com/go-quicktest/qt"
)

// The cmd package exposes some APIs, which some users rely on.
// Ensure that they continue to work as advertised.

func TestCommand(t *testing.T) {
ctx := context.Background()

// Create one command and run it, only checking that it succeeds.
c, err := cmd.New([]string{"help", "export"})
c.SetOutput(io.Discard)
qt.Assert(t, qt.IsNil(err))
err = c.Run(ctx)
qt.Assert(t, qt.IsNil(err))

// Create another command and run it, expecting it to fail.
c, err = cmd.New([]string{"help", "nosuchcommand"})
c.SetOutput(io.Discard)
qt.Assert(t, qt.IsNil(err))
err = c.Run(ctx)
qt.Assert(t, qt.IsNotNil(err))

// Verify that SetInput and SetOutput work.
c, err = cmd.New([]string{"export", "-"})
c.SetInput(strings.NewReader("foo: 123\n"))
var buf bytes.Buffer
c.SetOutput(&buf)
qt.Assert(t, qt.IsNil(err))
err = c.Run(ctx)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(buf.String(), "{\n \"foo\": 123\n}\n"))

// Verify that we can use the API exposed by the embedded cobra command.
// TODO(mvdan): panics due to https://cuelang.org/issue/3458; fix it.
qt.Assert(t, qt.PanicMatches(func() {
c, err = cmd.New([]string{"fmt", "nosuchfile.cue"})
err = c.Execute()
qt.Assert(t, qt.IsNotNil(err))
}, "cmd/cue/cmd.mkRunE init ran twice"))
}

0 comments on commit 1f8fb57

Please sign in to comment.