File: file_test.go

package info (click to toggle)
golang-github-azuread-microsoft-authentication-extensions-for-go 0.0~git20231002.7e3b8e2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,438 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

package file

import (
	"context"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
)

var ctx = context.Background()

func TestReadWriteDelete(t *testing.T) {
	for _, test := range []struct {
		desc              string
		initialData, want []byte
	}{
		{desc: "Test when the file exists", initialData: []byte("data"), want: []byte("want")},
		{desc: "Test when the file doesn't exist", want: []byte("want")},
	} {
		t.Run(test.desc, func(t *testing.T) {
			p := filepath.Join(t.TempDir(), t.Name())
			if test.initialData != nil {
				require.NoError(t, os.MkdirAll(filepath.Dir(p), 0700))
				f, err := os.OpenFile(p, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0600)
				require.NoError(t, err)
				_, err = f.Write(test.initialData)
				require.NoError(t, err)
				require.NoError(t, f.Close())
			}
			a, err := New(p)
			require.NoError(t, err)

			if test.initialData != nil {
				actual, err := a.Read(ctx)
				require.NoError(t, err)
				require.Equal(t, test.initialData, actual)
			}

			cp := make([]byte, len(test.want))
			copy(cp, test.want)
			err = a.Write(ctx, cp)
			require.NoError(t, err)

			actual, err := a.Read(ctx)
			require.NoError(t, err)
			require.Equal(t, test.want, actual)

			require.NoError(t, a.Delete(context.Background()))
		})
	}
}