github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/cmd/rename_integration_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/qri-io/dataset"
    10  	"github.com/qri-io/qfs"
    11  	"github.com/qri-io/qri/dsref"
    12  )
    13  
    14  // Test that rename command only works with human-friendly references, those without paths
    15  func TestRenameNeedsHumanName(t *testing.T) {
    16  	run := NewTestRunner(t, "test_peer_rename_human", "rename_human")
    17  	defer run.Delete()
    18  
    19  	// Create a dataset and get the resolved reference to it
    20  	output := run.MustExecCombinedOutErr(t, "qri save --body=testdata/movies/body_ten.csv me/first_name")
    21  	ref := dsref.MustParse(parseRefFromSave(output))
    22  
    23  	if !strings.HasPrefix(ref.Path, "/ipfs/") {
    24  		t.Errorf("expected saved ref to start with '/ipfs/', but got %q", ref.Path)
    25  	}
    26  
    27  	// Parse error for the land-hand-side
    28  	err := run.ExecCommand("qri rename test_peer_rename_human/invalid+name test_peer_rename_human/second_name")
    29  	if err == nil {
    30  		t.Fatal("expected error, did not get one")
    31  	}
    32  	expectErr := `original name: unexpected character at position 30: '+'`
    33  	if diff := cmp.Diff(expectErr, errorMessage(err)); diff != "" {
    34  		t.Errorf("unexpected (-want +got):\n%s", diff)
    35  	}
    36  
    37  	lhs := ref.Copy()
    38  
    39  	// Given a resolved reference for the left-hand-side is an error
    40  	err = run.ExecCommand(fmt.Sprintf("qri rename %s test_peer_rename_human/second_name", lhs))
    41  	if err == nil {
    42  		t.Fatal("expected error, did not get one")
    43  	}
    44  	expectErr = `original name: unexpected character '@', ref can only have username/name`
    45  	if diff := cmp.Diff(expectErr, errorMessage(err)); diff != "" {
    46  		t.Errorf("unexpected (-want +got):\n%s", diff)
    47  	}
    48  
    49  	// Make left-hand-side into a human-friendly path
    50  	lhs.Path = ""
    51  
    52  	// Parse error for the right-hand-side
    53  	err = run.ExecCommand(fmt.Sprintf("qri rename %s test_peer_rename_human/invalid+name", lhs))
    54  	if err == nil {
    55  		t.Fatal("expected error, did not get one")
    56  	}
    57  	expectErr = `destination name: dataset name must start with a lower-case letter, and only contain lower-case letters, numbers, dashes, and underscore. Maximum length is 144 characters`
    58  	if diff := cmp.Diff(expectErr, errorMessage(err)); diff != "" {
    59  		t.Errorf("unexpected (-want +got):\n%s", diff)
    60  	}
    61  
    62  	// Create right-hand-side with a path
    63  	rhs := ref.Copy()
    64  	rhs.Name = "second_name"
    65  
    66  	// Given a resolved reference for the right-hand-side is an error
    67  	err = run.ExecCommand(fmt.Sprintf("qri rename %s %s", lhs, rhs))
    68  	if err == nil {
    69  		t.Fatal("expected error, did not get one")
    70  	}
    71  	expectErr = `destination name: unexpected character '@', ref can only have username/name`
    72  	if diff := cmp.Diff(expectErr, errorMessage(err)); diff != "" {
    73  		t.Errorf("unexpected (-want +got):\n%s", diff)
    74  	}
    75  
    76  	// Make right-hand-side into a human-friendly path
    77  	rhs.Path = ""
    78  
    79  	// Now the rename command works without error
    80  	err = run.ExecCommand(fmt.Sprintf("qri rename %s %s", lhs, rhs))
    81  	if err != nil {
    82  		t.Errorf("got error: %s", err)
    83  	}
    84  }
    85  
    86  // Test that rename can be used on names with bad upper-case characters, but only to rename them
    87  // to be valid instead
    88  func TestRenameAwayFromBadCase(t *testing.T) {
    89  	run := NewTestRunner(t, "test_peer_rename_away_from_bad_case", "rename_away_from_bad_case")
    90  	defer run.Delete()
    91  
    92  	// Create a dataset with a valid name
    93  	run.MustExec(t, "qri save --body=testdata/movies/body_ten.csv me/first_name")
    94  
    95  	// Cannot rename the dataset to a name with bad upper-case characters
    96  	err := run.ExecCommand("qri rename test_peer_rename_away_from_bad_case/first_name test_peer_rename_away_from_bad_case/useUpperCase")
    97  	if err == nil {
    98  		t.Fatal("expected error, did not get one")
    99  	}
   100  	expectErr := `destination name: dataset name must start with a lower-case letter, and only contain lower-case letters, numbers, dashes, and underscore. Maximum length is 144 characters`
   101  	if diff := cmp.Diff(expectErr, errorMessage(err)); diff != "" {
   102  		t.Errorf("unexpected (-want +got):\n%s", diff)
   103  	}
   104  
   105  	// Construct a dataset
   106  	ds := dataset.Dataset{
   107  		Structure: &dataset.Structure{
   108  			Format: "json",
   109  			Schema: dataset.BaseSchemaArray,
   110  		},
   111  	}
   112  	ds.SetBodyFile(qfs.NewMemfileBytes("body.json", []byte("[[\"one\",2],[\"three\",4]]")))
   113  
   114  	// Add the dataset to the repo directly, which avoids the name validation check.
   115  	run.AddDatasetToRefstore(t, "test_peer_rename_away_from_bad_case/a_New_Dataset", &ds)
   116  
   117  	// Cannot rename the dataset to a name with bad upper-case characters still
   118  	err = run.ExecCommand("qri rename test_peer_rename_away_from_bad_case/a_New_Dataset test_peer_rename_away_from_bad_case/useUpperCase")
   119  	if err == nil {
   120  		t.Fatal("expected error, did not get one")
   121  	}
   122  	expectErr = `destination name: dataset name must start with a lower-case letter, and only contain lower-case letters, numbers, dashes, and underscore. Maximum length is 144 characters`
   123  	if diff := cmp.Diff(expectErr, errorMessage(err)); diff != "" {
   124  		t.Errorf("unexpected (-want +got):\n%s", diff)
   125  	}
   126  
   127  	// Okay to rename a name with bad upper-case characters to a new valid name
   128  	err = run.ExecCommand("qri rename test_peer_rename_away_from_bad_case/a_New_Dataset test_peer_rename_away_from_bad_case/a_new_dataset")
   129  	if err != nil {
   130  		t.Errorf("got error: %s", err)
   131  	}
   132  }