Document that --json skips the confirmation prompt (closes #268)
check / check (push) Waiting to run
check / check (push) Waiting to run
snapshot remove and prune delete without asking under --json, because a prompt on stdout would break the JSON document. Their --json help and README entries named only --force as skipping the prompt. Both now say --json skips it too. One test checks the --json help of both commands; another runs prune with --json, no --force and empty stdin, and checks that the unreferenced blob is deleted and stdout holds only the JSON document. Model: opus-5-5
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package cli //nolint:testpackage // exercises the unexported command constructor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestJSONHelpSaysConfirmationPromptIsSkipped checks that the --json help
|
||||
// of `snapshot remove` and `prune` says the flag skips the confirmation
|
||||
// prompt. Both delete without asking under --json, since a prompt on
|
||||
// stdout would break the JSON document.
|
||||
func TestJSONHelpSaysConfirmationPromptIsSkipped(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, cmd := range []*cobra.Command{
|
||||
newSnapshotRemoveCommand(),
|
||||
NewPruneCommand(),
|
||||
} {
|
||||
assert.Contains(t, cmd.Flags().Lookup("json").Usage,
|
||||
"skips the confirmation prompt", cmd.Name())
|
||||
}
|
||||
}
|
||||
@@ -57,7 +57,8 @@ referenced.`,
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&opts.Force, "force", false, "Skip confirmation prompt")
|
||||
cmd.Flags().BoolVar(&opts.JSON, "json", false, "Output pruning stats as JSON")
|
||||
cmd.Flags().BoolVar(&opts.JSON, "json", false,
|
||||
"Output pruning stats as JSON; skips the confirmation prompt, as --force does")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -280,7 +280,8 @@ nuke --force' — it is the single supported entry point for that.`,
|
||||
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Skip confirmation prompt")
|
||||
cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false,
|
||||
"Show what would be removed without removing")
|
||||
cmd.Flags().BoolVar(&opts.JSON, "json", false, "Output result as JSON")
|
||||
cmd.Flags().BoolVar(&opts.JSON, "json", false,
|
||||
"Output result as JSON; skips the confirmation prompt, as --force does")
|
||||
cmd.Flags().BoolVar(&opts.LocalOnly, "local-only", false,
|
||||
"Skip remote cleanup; only touch the local index")
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package vaultik_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"sneak.berlin/go/vaultik/internal/log"
|
||||
"sneak.berlin/go/vaultik/internal/vaultik"
|
||||
)
|
||||
|
||||
// TestPruneBlobs_JSONDeletesWithoutAsking checks that prune with --json
|
||||
// and without --force deletes an unreferenced blob without the
|
||||
// confirmation prompt. Stdin is empty, so a prompt would read no answer
|
||||
// and cancel, and its text would come before the JSON document.
|
||||
func TestPruneBlobs_JSONDeletesWithoutAsking(t *testing.T) {
|
||||
log.Initialize(log.Config{})
|
||||
t.Parallel()
|
||||
|
||||
env := newListEnv(t)
|
||||
|
||||
// The store holds no manifest, so nothing references this blob.
|
||||
addBlob(t, env.store.testStorer, testBlobHashA)
|
||||
|
||||
err := env.v.PruneBlobs(&vaultik.PruneOptions{JSON: true})
|
||||
require.NoError(t, err)
|
||||
|
||||
blobKey := "blobs/" + testBlobHashA[:2] + "/" + testBlobHashA[2:4] +
|
||||
"/" + testBlobHashA
|
||||
assert.False(t, env.store.hasKey(blobKey),
|
||||
"the unreferenced blob must be deleted")
|
||||
|
||||
var result vaultik.PruneBlobsResult
|
||||
|
||||
require.NoError(t, json.Unmarshal(env.stdout.Bytes(), &result),
|
||||
"stdout must hold only the JSON document, got:\n%s",
|
||||
env.stdout.String())
|
||||
assert.Equal(t, 1, result.BlobsDeleted)
|
||||
}
|
||||
Reference in New Issue
Block a user