@@ -56,7 +56,9 @@ func Test_rootCommand(t *testing.T) {
5656 require .NoError (t , err )
5757 impl , ok := s .(* pass.PassValue )
5858 require .True (t , ok )
59- assert .Equal (t , "bar=bar=bar" , string (impl .Value ))
59+ v , err := impl .Marshal ()
60+ require .NoError (t , err )
61+ assert .Equal (t , "bar=bar=bar" , string (v ))
6062 })
6163 t .Run ("from STDIN" , func (t * testing.T ) {
6264 mock := teststore .NewMockStore ()
@@ -67,7 +69,56 @@ func Test_rootCommand(t *testing.T) {
6769 require .NoError (t , err )
6870 impl , ok := s .(* pass.PassValue )
6971 require .True (t , ok )
70- assert .Equal (t , "my\n multiline\n value" , string (impl .Value ))
72+ v , err := impl .Marshal ()
73+ require .NoError (t , err )
74+ assert .Equal (t , "my\n multiline\n value" , string (v ))
75+ })
76+ t .Run ("with --metadata flag" , func (t * testing.T ) {
77+ mock := teststore .NewMockStore ()
78+ out , err := executeCommand (Root (t .Context (), mock , mockInfo ), "set" , "foo=bar" , "--metadata" , "name=bob" , "--metadata" , "expiry=2027-03-01" )
79+ assert .NoError (t , err )
80+ assert .Empty (t , out )
81+ s , err := mock .Get (t .Context (), secrets .MustParseID ("foo" ))
82+ require .NoError (t , err )
83+ impl , ok := s .(* pass.PassValue )
84+ require .True (t , ok )
85+ v , err := impl .Marshal ()
86+ require .NoError (t , err )
87+ assert .Equal (t , "bar" , string (v ))
88+ assert .Equal (t , map [string ]string {"name" : "bob" , "expiry" : "2027-03-01" }, impl .Metadata ())
89+ })
90+ t .Run ("from STDIN JSON with value and metadata" , func (t * testing.T ) {
91+ mock := teststore .NewMockStore ()
92+ out , err := executeCommandWithStdin (Root (t .Context (), mock , mockInfo ), `{"secret":"bar","metadata":{"name":"bob"}}` , "set" , "foo" )
93+ assert .NoError (t , err )
94+ assert .Empty (t , out )
95+ s , err := mock .Get (t .Context (), secrets .MustParseID ("foo" ))
96+ require .NoError (t , err )
97+ impl , ok := s .(* pass.PassValue )
98+ require .True (t , ok )
99+ v , err := impl .Marshal ()
100+ require .NoError (t , err )
101+ assert .Equal (t , "bar" , string (v ))
102+ assert .Equal (t , map [string ]string {"name" : "bob" }, impl .Metadata ())
103+ })
104+ t .Run ("from STDIN JSON merged with --metadata flag wins on collision" , func (t * testing.T ) {
105+ mock := teststore .NewMockStore ()
106+ out , err := executeCommandWithStdin (Root (t .Context (), mock , mockInfo ), `{"secret":"bar","metadata":{"name":"bob","extra":"thing"}}` , "set" , "foo" , "--metadata" , "name=alice" )
107+ assert .NoError (t , err )
108+ assert .Empty (t , out )
109+ s , err := mock .Get (t .Context (), secrets .MustParseID ("foo" ))
110+ require .NoError (t , err )
111+ impl , ok := s .(* pass.PassValue )
112+ require .True (t , ok )
113+ v , err := impl .Marshal ()
114+ require .NoError (t , err )
115+ assert .Equal (t , "bar" , string (v ))
116+ assert .Equal (t , map [string ]string {"name" : "alice" , "extra" : "thing" }, impl .Metadata ())
117+ })
118+ t .Run ("invalid --metadata flag (no =)" , func (t * testing.T ) {
119+ mock := teststore .NewMockStore ()
120+ _ , err := executeCommand (Root (t .Context (), mock , mockInfo ), "set" , "foo=bar" , "--metadata" , "invalid" )
121+ assert .ErrorContains (t , err , "invalid metadata pair (expected key=value): invalid" )
71122 })
72123 t .Run ("store error" , func (t * testing.T ) {
73124 errSave := errors .New ("save error" )
@@ -88,8 +139,8 @@ func Test_rootCommand(t *testing.T) {
88139 t .Run ("list" , func (t * testing.T ) {
89140 t .Run ("ok" , func (t * testing.T ) {
90141 mock := teststore .NewMockStore (teststore .WithStore (map [store.ID ]store.Secret {
91- store .MustParseID ("foo" ): & pass.PassValue { Value : []byte ("bar" )} ,
92- store .MustParseID ("baz" ): & pass.PassValue { Value : []byte ("0" )} ,
142+ store .MustParseID ("foo" ): pass .NewPassValue ( []byte ("bar" )) ,
143+ store .MustParseID ("baz" ): pass .NewPassValue ( []byte ("0" )) ,
93144 }))
94145 out , err := executeCommand (Root (t .Context (), mock , mockInfo ), "list" )
95146 assert .NoError (t , err )
@@ -106,8 +157,8 @@ func Test_rootCommand(t *testing.T) {
106157 t .Run ("rm" , func (t * testing.T ) {
107158 t .Run ("ok (two secrets)" , func (t * testing.T ) {
108159 mock := teststore .NewMockStore (teststore .WithStore (map [store.ID ]store.Secret {
109- store .MustParseID ("foo" ): & pass.PassValue { Value : []byte ("bar" )} ,
110- store .MustParseID ("baz" ): & pass.PassValue { Value : []byte ("0" )} ,
160+ store .MustParseID ("foo" ): pass .NewPassValue ( []byte ("bar" )) ,
161+ store .MustParseID ("baz" ): pass .NewPassValue ( []byte ("0" )) ,
111162 }))
112163 out , err := executeCommand (Root (t .Context (), mock , mockInfo ), "rm" , "foo" , "baz" )
113164 assert .NoError (t , err )
@@ -118,8 +169,8 @@ func Test_rootCommand(t *testing.T) {
118169 })
119170 t .Run ("--all" , func (t * testing.T ) {
120171 mock := teststore .NewMockStore (teststore .WithStore (map [store.ID ]store.Secret {
121- store .MustParseID ("foo" ): & pass.PassValue { Value : []byte ("bar" )} ,
122- store .MustParseID ("baz" ): & pass.PassValue { Value : []byte ("0" )} ,
172+ store .MustParseID ("foo" ): pass .NewPassValue ( []byte ("bar" )) ,
173+ store .MustParseID ("baz" ): pass .NewPassValue ( []byte ("0" )) ,
123174 }))
124175 out , err := executeCommand (Root (t .Context (), mock , mockInfo ), "rm" , "--all" )
125176 assert .NoError (t , err )
@@ -158,7 +209,7 @@ func Test_rootCommand(t *testing.T) {
158209 t .Run ("get" , func (t * testing.T ) {
159210 t .Run ("ok" , func (t * testing.T ) {
160211 mock := teststore .NewMockStore (teststore .WithStore (map [store.ID ]store.Secret {
161- store .MustParseID ("foo" ): & pass.PassValue { Value : []byte ("bar" )} ,
212+ store .MustParseID ("foo" ): pass .NewPassValue ( []byte ("bar" )) ,
162213 }))
163214 out , err := executeCommand (Root (t .Context (), mock , mockInfo ), "get" , "foo" )
164215 assert .NoError (t , err )
@@ -200,7 +251,7 @@ func Test_rootCommandTelemetry(t *testing.T) {
200251 t .Run (tc .name , func (t * testing.T ) {
201252 spanRecorder , metricReader := testhelper .SetupTelemetry (t )
202253 mock := teststore .NewMockStore (teststore .WithStore (map [store.ID ]store.Secret {
203- store .MustParseID ("baz" ): & pass.PassValue { Value : []byte ("bar" )} ,
254+ store .MustParseID ("baz" ): pass .NewPassValue ( []byte ("bar" )) ,
204255 }))
205256 _ , err := executeCommand (Root (t .Context (), mock , mockInfo ), tc .args ... )
206257 assert .NoError (t , err )
0 commit comments