MCPcopy
hub / github.com/cli/cli / TestGetOrCreateDeviceID

Function TestGetOrCreateDeviceID

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

Source from the content-addressed store, hash-verified

63}
64
65func TestGetOrCreateDeviceID(t *testing.T) {
66 t.Run("creates new ID on first call", func(t *testing.T) {
67 tmpDir := t.TempDir()
68 t.Cleanup(stubStateDir(tmpDir))
69
70 id, err := getOrCreateDeviceID()
71 require.NoError(t, err)
72 require.NotEmpty(t, id)
73
74 data, err := os.ReadFile(filepath.Join(tmpDir, deviceIDFileName))
75 require.NoError(t, err)
76 assert.Equal(t, id, string(data))
77 })
78
79 t.Run("returns same ID on subsequent calls", func(t *testing.T) {
80 tmpDir := t.TempDir()
81 t.Cleanup(stubStateDir(tmpDir))
82
83 id1, err := getOrCreateDeviceID()
84 require.NoError(t, err)
85
86 id2, err := getOrCreateDeviceID()
87 require.NoError(t, err)
88
89 assert.Equal(t, id1, id2)
90 })
91
92 t.Run("trims whitespace from stored ID", func(t *testing.T) {
93 tmpDir := t.TempDir()
94 t.Cleanup(stubStateDir(tmpDir))
95
96 err := os.WriteFile(filepath.Join(tmpDir, deviceIDFileName), []byte(" some-device-id\n"), 0o600)
97 require.NoError(t, err)
98
99 id, err := getOrCreateDeviceID()
100 require.NoError(t, err)
101 assert.Equal(t, "some-device-id", id)
102 })
103
104 t.Run("returns error for non-ErrNotExist read failures", func(t *testing.T) {
105 tmpDir := t.TempDir()
106 t.Cleanup(stubStateDir(tmpDir))
107
108 // Create device-id as a directory so ReadFile fails with a non-ErrNotExist error.
109 err := os.Mkdir(filepath.Join(tmpDir, deviceIDFileName), 0o755)
110 require.NoError(t, err)
111
112 _, err = getOrCreateDeviceID()
113 require.Error(t, err)
114 assert.False(t, errors.Is(err, os.ErrNotExist))
115 })
116
117 t.Run("creates state directory if missing", func(t *testing.T) {
118 tmpDir := t.TempDir()
119 nestedDir := filepath.Join(tmpDir, "nested", "state")
120 t.Cleanup(stubStateDir(nestedDir))
121
122 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