@@ -2,11 +2,14 @@ package serpent_test
2
2
3
3
import (
4
4
"fmt"
5
+ "io"
5
6
"os"
7
+ "path/filepath"
6
8
"strings"
7
9
"testing"
8
10
9
11
serpent "github.com/coder/serpent"
12
+ "github.com/coder/serpent/completion"
10
13
"github.com/stretchr/testify/require"
11
14
)
12
15
@@ -231,3 +234,67 @@ func TestFileCompletion(t *testing.T) {
231
234
})
232
235
}
233
236
}
237
+
238
+ func TestCompletionInstall (t * testing.T ) {
239
+ t .Parallel ()
240
+
241
+ t .Run ("InstallingAppend" , func (t * testing.T ) {
242
+ dir := t .TempDir ()
243
+ path := filepath .Join (dir , "fake.sh" )
244
+ f , err := os .Create (path )
245
+ require .NoError (t , err )
246
+ f .Write ([]byte ("FAKE_SCRIPT" ))
247
+ f .Close ()
248
+
249
+ shell := & fakeShell {baseInstallDir : dir , useOwn : false }
250
+ err = completion .InstallShellCompletion (shell )
251
+ require .NoError (t , err )
252
+ contents , err := os .ReadFile (path )
253
+ require .NoError (t , err )
254
+ require .Equal (t , "FAKE_SCRIPTFAKE_COMPLETION" , string (contents ))
255
+ })
256
+
257
+ t .Run ("InstallReplace" , func (t * testing.T ) {
258
+ dir := t .TempDir ()
259
+ path := filepath .Join (dir , "fake.sh" )
260
+ f , err := os .Create (path )
261
+ require .NoError (t , err )
262
+ f .Write ([]byte ("FAKE_SCRIPT" ))
263
+ f .Close ()
264
+
265
+ shell := & fakeShell {baseInstallDir : dir , useOwn : true }
266
+ err = completion .InstallShellCompletion (shell )
267
+ require .NoError (t , err )
268
+ contents , err := os .ReadFile (path )
269
+ require .NoError (t , err )
270
+ require .Equal (t , "FAKE_COMPLETION" , string (contents ))
271
+ })
272
+ }
273
+
274
+ type fakeShell struct {
275
+ baseInstallDir string
276
+ useOwn bool
277
+ }
278
+
279
+ var _ completion.Shell = & fakeShell {}
280
+
281
+ // InstallPath implements completion.Shell.
282
+ func (f * fakeShell ) InstallPath () (string , error ) {
283
+ return filepath .Join (f .baseInstallDir , "fake.sh" ), nil
284
+ }
285
+
286
+ // Name implements completion.Shell.
287
+ func (f * fakeShell ) Name () string {
288
+ return "fake"
289
+ }
290
+
291
+ // UsesOwnFile implements completion.Shell.
292
+ func (f * fakeShell ) UsesOwnFile () bool {
293
+ return f .useOwn
294
+ }
295
+
296
+ // WriteCompletion implements completion.Shell.
297
+ func (f * fakeShell ) WriteCompletion (w io.Writer ) error {
298
+ _ , err := w .Write ([]byte ("FAKE_COMPLETION" ))
299
+ return err
300
+ }
0 commit comments