From 1f8fb572b186d66b53ddd1d8b3b33f0f1e7a1555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 16 Oct 2024 16:20:54 +0100 Subject: [PATCH] cmd/cue/cmd: add a Go test covering the exposed API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: I80fa81c3f3d700fc575f3b337abae11ae55ea8de Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202645 TryBot-Result: CUEcueckoo Reviewed-by: Roger Peppe Unity-Result: CUE porcuepine --- cmd/cue/cmd/root_test.go | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 cmd/cue/cmd/root_test.go diff --git a/cmd/cue/cmd/root_test.go b/cmd/cue/cmd/root_test.go new file mode 100644 index 00000000000..648e55a82ce --- /dev/null +++ b/cmd/cue/cmd/root_test.go @@ -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")) +}