Skip to content

Commit f34a6b4

Browse files
marcpopMSFTgithub-actions
authored andcommitted
fix the new deviceid tests Make sure we return an empty string if caching fails but don't error our code refactor the caching code slightly for simplicity
1 parent a97a233 commit f34a6b4

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Cli/dotnet/Telemetry/DevDeviceIDGetter.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ public static string GetDeviceId()
1818
deviceId = Guid.NewGuid().ToString("D").ToLowerInvariant();
1919

2020
// Cache the new device Id
21-
CacheDeviceId(deviceId);
21+
try
22+
{
23+
CacheDeviceId(deviceId);
24+
}
25+
catch
26+
{
27+
// If caching fails, return empty string to avoid sending a non-stored id
28+
deviceId = ""
29+
}
2230
}
2331

2432
return deviceId;
@@ -92,14 +100,25 @@ private static void CacheDeviceId(string deviceId)
92100
cacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cache", "deviceid");
93101
}
94102

95-
File.WriteAllText(cacheFilePath, deviceId);
103+
CreateDirectoryAndWriteToFile(cacheFilePath, deviceId);
96104
}
97105
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
98106
{
99107
// Cache device Id in macOS cache file
100108
string cacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Application Support", "Microsoft", "DeveloperTools", "deviceid");
101-
File.WriteAllText(cacheFilePath, deviceId);
109+
110+
CreateDirectoryAndWriteToFile(cacheFilePath, deviceId);
111+
}
112+
}
113+
114+
private static void CreateDirectoryAndWriteToFile(string filePath, string content)
115+
{
116+
string directory = Path.GetDirectoryName(filePath);
117+
if (!Directory.Exists(directory))
118+
{
119+
Directory.CreateDirectory(directory);
102120
}
121+
File.WriteAllText(filePath, content);
103122
}
104123
}
105124
}

src/Cli/dotnet/Telemetry/TelemetryCommonProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public TelemetryCommonProperties(
4343
private const string ProductVersion = "Product Version";
4444
private const string TelemetryProfile = "Telemetry Profile";
4545
private const string CurrentPathHash = "Current Path Hash";
46-
private const string DeviceId = "DeviceId";
46+
private const string DeviceId = "devdeviceid";
4747
private const string MachineId = "Machine ID";
4848
private const string MachineIdOld = "Machine ID Old";
4949
private const string DockerContainer = "Docker Container";

src/Tests/dotnet.Tests/TelemetryCommonPropertiesTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void TelemetryCommonPropertiesShouldReturnHashedMachineId()
3737
public void TelemetryCommonPropertiesShouldReturnDevDeviceId()
3838
{
3939
var unitUnderTest = new TelemetryCommonProperties(getDeviceId: () => "plaintext", userLevelCacheWriter: new NothingCache());
40-
unitUnderTest.GetTelemetryCommonProperties()["DevDeviceId"].Should().Be("plaintext");
40+
unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"].Should().Be("plaintext");
4141
}
4242

4343
[Fact]
@@ -52,8 +52,8 @@ public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddress(
5252
[Fact]
5353
public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotDevDeviceId()
5454
{
55-
var unitUnderTest = new TelemetryCommonProperties(getDeviceId: () => null, userLevelCacheWriter: new NothingCache());
56-
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["DevDeviceId"];
55+
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
56+
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"];
5757

5858
Guid.TryParse(assignedMachineId, out var _).Should().BeTrue("it should be a guid");
5959
}

0 commit comments

Comments
 (0)