github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/cmd/noms/noms_commit_test.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package main 6 7 import ( 8 "testing" 9 10 "github.com/attic-labs/noms/go/datas" 11 "github.com/attic-labs/noms/go/spec" 12 "github.com/attic-labs/noms/go/types" 13 "github.com/attic-labs/noms/go/util/clienttest" 14 "github.com/stretchr/testify/suite" 15 ) 16 17 type nomsCommitTestSuite struct { 18 clienttest.ClientTestSuite 19 } 20 21 func TestNomsCommit(t *testing.T) { 22 suite.Run(t, &nomsCommitTestSuite{}) 23 } 24 25 func (s *nomsCommitTestSuite) setupDataset(name string, doCommit bool) (sp spec.Spec, ref types.Ref) { 26 var err error 27 sp, err = spec.ForDataset(spec.CreateValueSpecString("nbs", s.DBDir, name)) 28 s.NoError(err) 29 30 v := types.String("testcommit") 31 ref = sp.GetDatabase().WriteValue(v) 32 33 if doCommit { 34 _, err = sp.GetDatabase().CommitValue(sp.GetDataset(), ref) 35 s.NoError(err) 36 } 37 return 38 } 39 40 func (s *nomsCommitTestSuite) TestNomsCommitToDatasetWithoutHead() { 41 sp, ref := s.setupDataset("commitTest", false) 42 defer sp.Close() 43 44 _, ok := sp.GetDataset().MaybeHead() 45 s.False(ok, "should not have a commit") 46 47 stdoutString, stderrString := s.MustRun(main, []string{"commit", "#" + ref.TargetHash().String(), sp.String()}) 48 s.Empty(stderrString) 49 s.Contains(stdoutString, "New head #") 50 51 sp, _ = spec.ForDataset(sp.String()) 52 defer sp.Close() 53 54 commit, ok := sp.GetDataset().MaybeHead() 55 s.True(ok, "should have a commit now") 56 value := commit.Get(datas.ValueField) 57 s.True(value.Hash() == ref.TargetHash(), "commit.value hash == writevalue hash") 58 59 meta := commit.Get(datas.MetaField).(types.Struct) 60 s.NotEmpty(meta.Get("date")) 61 } 62 63 func structFieldEqual(old, now types.Struct, field string) bool { 64 oldValue, oldOk := old.MaybeGet(field) 65 nowValue, nowOk := now.MaybeGet(field) 66 return oldOk && nowOk && nowValue.Equals(oldValue) 67 } 68 69 func (s *nomsCommitTestSuite) runDuplicateTest(allowDuplicate bool) { 70 dsName := "commitTestDuplicate" 71 sp, ref := s.setupDataset(dsName, true) 72 defer sp.Close() 73 74 _, ok := sp.GetDataset().MaybeHeadValue() 75 s.True(ok, "should have a commit") 76 77 cliOptions := []string{"commit"} 78 if allowDuplicate { 79 cliOptions = append(cliOptions, "--allow-dupe") 80 } 81 cliOptions = append(cliOptions, dsName+".value", sp.String()) 82 83 stdoutString, stderrString, err := s.Run(main, cliOptions) 84 s.Nil(err) 85 s.Empty(stderrString) 86 if allowDuplicate { 87 s.NotContains(stdoutString, "Commit aborted") 88 s.Contains(stdoutString, "New head #") 89 } else { 90 s.Contains(stdoutString, "Commit aborted") 91 } 92 93 sp, _ = spec.ForDataset(sp.String()) 94 defer sp.Close() 95 96 value, ok := sp.GetDataset().MaybeHeadValue() 97 s.True(ok, "should still have a commit") 98 s.True(value.Hash() == ref.Hash(), "commit.value hash == previous commit hash") 99 } 100 101 func (s *nomsCommitTestSuite) TestNomsCommitDuplicate() { 102 s.runDuplicateTest(false) 103 s.runDuplicateTest(true) 104 } 105 106 func (s *nomsCommitTestSuite) TestNomsCommitMetadata() { 107 dsName := "commitTestMetadata" 108 sp, _ := s.setupDataset(dsName, true) 109 defer sp.Close() 110 111 metaOld := sp.GetDataset().Head().Get(datas.MetaField).(types.Struct) 112 113 stdoutString, stderrString, err := s.Run(main, []string{"commit", "--allow-dupe", "--message=foo", dsName + ".value", sp.String()}) 114 s.Nil(err) 115 s.Empty(stderrString) 116 s.Contains(stdoutString, "New head #") 117 118 sp, _ = spec.ForDataset(sp.String()) 119 defer sp.Close() 120 121 metaNew := sp.GetDataset().Head().Get(datas.MetaField).(types.Struct) 122 123 s.False(metaOld.Equals(metaNew), "meta didn't change") 124 s.False(structFieldEqual(metaOld, metaNew, "date"), "date didn't change") 125 s.False(structFieldEqual(metaOld, metaNew, "message"), "message didn't change") 126 s.True(metaNew.Get("message").Equals(types.String("foo")), "message wasn't set") 127 128 metaOld = metaNew 129 130 stdoutString, stderrString = s.MustRun(main, []string{"commit", "--allow-dupe", "--message=bar", "--date=" + spec.CommitMetaDateFormat[:20], dsName + ".value", sp.String()}) 131 s.Empty(stderrString) 132 s.Contains(stdoutString, "New head #") 133 134 sp, _ = spec.ForDataset(sp.String()) 135 defer sp.Close() 136 137 metaNew = sp.GetDataset().Head().Get(datas.MetaField).(types.Struct) 138 139 s.False(metaOld.Equals(metaNew), "meta didn't change") 140 s.False(structFieldEqual(metaOld, metaNew, "date"), "date didn't change") 141 s.False(structFieldEqual(metaOld, metaNew, "message"), "message didn't change") 142 s.True(metaNew.Get("message").Equals(types.String("bar")), "message wasn't set") 143 } 144 145 func (s *nomsCommitTestSuite) TestNomsCommitHashNotFound() { 146 sp, _ := s.setupDataset("commitTestBadHash", true) 147 defer sp.Close() 148 149 s.Panics(func() { 150 s.MustRun(main, []string{"commit", "#9ei6fbrs0ujo51vifd3f2eebufo4lgdu", sp.String()}) 151 }) 152 } 153 154 func (s *nomsCommitTestSuite) TestNomsCommitMetadataBadDateFormat() { 155 sp, ref := s.setupDataset("commitTestMetadata", true) 156 defer sp.Close() 157 158 s.Panics(func() { 159 s.MustRun(main, []string{"commit", "--allow-dupe", "--date=a", "#" + ref.TargetHash().String(), sp.String()}) 160 }) 161 }