MCPcopy Create free account
hub / github.com/cli/cli / TestGetOrCreateDeviceID

Function TestGetOrCreateDeviceID

internal/telemetry/telemetry_test.go:41–131  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

39}
40
41func TestGetOrCreateDeviceID(t *testing.T) {
42 t.Run("creates new ID on first call", func(t *testing.T) {
43 tmpDir := t.TempDir()
44 t.Cleanup(stubStateDir(tmpDir))
45
46 id, err := getOrCreateDeviceID()
47 require.NoError(t, err)
48 require.NotEmpty(t, id)
49
50 data, err := os.ReadFile(filepath.Join(tmpDir, deviceIDFileName))
51 require.NoError(t, err)
52 assert.Equal(t, id, string(data))
53 })
54
55 t.Run("returns same ID on subsequent calls", func(t *testing.T) {
56 tmpDir := t.TempDir()
57 t.Cleanup(stubStateDir(tmpDir))
58
59 id1, err := getOrCreateDeviceID()
60 require.NoError(t, err)
61
62 id2, err := getOrCreateDeviceID()
63 require.NoError(t, err)
64
65 assert.Equal(t, id1, id2)
66 })
67
68 t.Run("trims whitespace from stored ID", func(t *testing.T) {
69 tmpDir := t.TempDir()
70 t.Cleanup(stubStateDir(tmpDir))
71
72 err := os.WriteFile(filepath.Join(tmpDir, deviceIDFileName), []byte(" some-device-id\n"), 0o600)
73 require.NoError(t, err)
74
75 id, err := getOrCreateDeviceID()
76 require.NoError(t, err)
77 assert.Equal(t, "some-device-id", id)
78 })
79
80 t.Run("returns error for non-ErrNotExist read failures", func(t *testing.T) {
81 tmpDir := t.TempDir()
82 t.Cleanup(stubStateDir(tmpDir))
83
84 // Create device-id as a directory so ReadFile fails with a non-ErrNotExist error.
85 err := os.Mkdir(filepath.Join(tmpDir, deviceIDFileName), 0o755)
86 require.NoError(t, err)
87
88 _, err = getOrCreateDeviceID()
89 require.Error(t, err)
90 assert.False(t, errors.Is(err, os.ErrNotExist))
91 })
92
93 t.Run("creates state directory if missing", func(t *testing.T) {
94 tmpDir := t.TempDir()
95 nestedDir := filepath.Join(tmpDir, "nested", "state")
96 t.Cleanup(stubStateDir(nestedDir))
97
98 id, err := getOrCreateDeviceID()

Callers

nothing calls this directly

Calls 9

stubStateDirFunction · 0.85
getOrCreateDeviceIDFunction · 0.85
JoinMethod · 0.80
EqualMethod · 0.80
RunMethod · 0.65
AddMethod · 0.65
DoneMethod · 0.65
WaitMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected