Skip to content

Commit

Permalink
Fix CreateFromEnvVars to allow '=' in env var value.
Browse files Browse the repository at this point in the history
  • Loading branch information
briancr-ms committed Mar 5, 2024
1 parent ded020c commit cb1cda9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void CreateFromEnvFile_WithAllSettings()
Assert.False(cs.CleanSession);
Assert.Equal(32, cs.KeepAliveInSeconds);
Assert.Equal("sample_client", cs.ClientId);
Assert.Equal("sample_user", cs.Username);
Assert.Equal("sample_user/api-version=2024-01-01", cs.Username);
Assert.Equal("foo", cs.Password);
Assert.Equal("ca.pem", cs.CaFile);
Assert.Equal("cert.pem", cs.CertFile);
Expand Down Expand Up @@ -132,13 +132,19 @@ private void RemoveTestEnvVars(string envFile)
{
foreach (var line in File.ReadAllLines(envFile))
{
var parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
int index = line.IndexOf('=');
if (index < 0)
{
continue;
}

Environment.SetEnvironmentVariable(parts[0], null);
string key = line[..index].Trim();
if (string.IsNullOrEmpty(key))
{
continue;
}

Environment.SetEnvironmentVariable(key, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MQTT_USE_TLS=false
MQTT_CLEAN_SESSION=false
MQTT_KEEP_ALIVE_IN_SECONDS=32
MQTT_CLIENT_ID=sample_client
MQTT_USERNAME=sample_user
MQTT_USERNAME=sample_user/api-version=2024-01-01
MQTT_PASSWORD=foo
MQTT_CA_FILE=ca.pem
MQTT_CERT_FILE=cert.pem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ public static MqttConnectionSettings CreateFromEnvVars(string? envFile = "")
Trace.TraceInformation("Loading environment variables from {envFile}" + new FileInfo(envFile).FullName);
foreach (string line in File.ReadAllLines(envFile))
{
string[] parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
int index = line.IndexOf('=');
if (index < 0)
{
continue;
}

Environment.SetEnvironmentVariable(parts[0], parts[1]);
string key = line[..index].Trim();
string value = line[(index + 1)..].Trim();
if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(key))
{
continue;
}

Environment.SetEnvironmentVariable(key, value);
}
}
else
Expand Down

0 comments on commit cb1cda9

Please sign in to comment.