Skip to content

Commit 8c442c5

Browse files
committed
Adding tests
1 parent f5e8d5b commit 8c442c5

File tree

3 files changed

+82
-15
lines changed

3 files changed

+82
-15
lines changed

src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,15 @@ public override async Task RunAsync()
273273
private void ValidateHostJsonConfiguration()
274274
{
275275
bool IsPreCompiledApp = IsPreCompiledFunctionApp();
276-
if (IsPreCompiledApp && !HostJsonExist())
276+
var hostJsonPath = Path.Combine(Environment.CurrentDirectory, Constants.HostJsonFileName);
277+
if (IsPreCompiledApp && !File.Exists(hostJsonPath))
277278
{
278-
throw new CliException($"Host.json file does not exist. Please make sure host.json file is preset at {Environment.CurrentDirectory}");
279+
throw new CliException($"Host.json file in missing. Please make sure host.json file is preset at {Environment.CurrentDirectory}");
279280
}
280281

281-
if (IsPreCompiledApp && BundleConfigurationExists())
282+
if (IsPreCompiledApp && BundleConfigurationExists(hostJsonPath))
282283
{
283-
throw new CliException($"Extension bundle is configured for the function app with pre-compiled functions. Please remove extension bundle configuration from host.json: {Path.Combine(Environment.CurrentDirectory, "host.json")}");
284+
throw new CliException($"Extension bundle configuration should not be present for the function app with pre-compiled functions. Please remove extension bundle configuration from host.json: {Path.Combine(Environment.CurrentDirectory, "host.json")}");
284285
}
285286
}
286287

@@ -317,30 +318,24 @@ private async Task PreRunConditions()
317318
}
318319
}
319320

320-
private bool HostJsonExist()
321+
private bool BundleConfigurationExists(string hostJsonPath)
321322
{
322-
var hostJsonPath = Path.Combine(Environment.CurrentDirectory, "host.json");
323-
return File.Exists(hostJsonPath);
324-
}
325-
326-
private bool BundleConfigurationExists()
327-
{
328-
var hostJsonPath = Path.Combine(Environment.CurrentDirectory, "host.json");
329323
var hostJson = FileSystemHelpers.ReadAllTextFromFile(hostJsonPath);
330-
return hostJson.Contains("extensionBundle", StringComparison.OrdinalIgnoreCase);
324+
return hostJson.Contains(Constants.ExtensionBundleConfigPropertyName, StringComparison.OrdinalIgnoreCase);
331325
}
332326

333327
private bool IsPreCompiledFunctionApp()
334328
{
335329
bool isPrecompiled = false;
336330
foreach (var directory in FileSystemHelpers.GetDirectories(Environment.CurrentDirectory))
337331
{
338-
var functionMetadataFile = Path.Combine(directory, "function.json");
332+
var functionMetadataFile = Path.Combine(directory, Constants.FunctionJsonFileName);
339333
if (File.Exists(functionMetadataFile))
340334
{
341335
var functionMetadataFileContent = FileSystemHelpers.ReadAllTextFromFile(functionMetadataFile);
342336
var functionMetadata = JsonConvert.DeserializeObject<FunctionMetadata>(functionMetadataFileContent);
343-
isPrecompiled = isPrecompiled || (functionMetadata?.ScriptFile?.EndsWith(".dll") != null && functionMetadata.ScriptFile.EndsWith(".dll"));
337+
string extension = Path.GetExtension(functionMetadata?.ScriptFile).ToLowerInvariant().TrimStart('.');
338+
isPrecompiled = isPrecompiled || (!string.IsNullOrEmpty(extension) && extension == "dll");
344339
}
345340
if (isPrecompiled)
346341
{

src/Azure.Functions.Cli/Common/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal static class Constants
2121
public const string FunctionsWorkerRuntimeVersion = "FUNCTIONS_WORKER_RUNTIME_VERSION";
2222
public const string RequirementsTxt = "requirements.txt";
2323
public const string FunctionJsonFileName = "function.json";
24+
public const string HostJsonFileName = "host.json";
2425
public const string ExtenstionsCsProjFile = "extensions.csproj";
2526
public const string DefaultVEnvName = "worker_env";
2627
public const string ExternalPythonPackages = ".python_packages";
@@ -45,6 +46,7 @@ internal static class Constants
4546
public const string DefaultManagementURL = "https://management.azure.com/";
4647
public const string AzureManagementAccessToken = "AZURE_MANAGEMENT_ACCESS_TOKEN";
4748
public const string AzureFunctionsEnvorinmentEnvironmentVariable = "AZURE_FUNCTIONS_ENVIRONMENT";
49+
public const string ExtensionBundleConfigPropertyName = "extensionBundle";
4850
public const string AspNetCoreEnvironmentEnvironmentVariable = "ASPNETCORE_ENVIRONMENT";
4951

5052
public static string CliVersion => typeof(Constants).GetTypeInfo().Assembly.GetName().Version.ToString(3);

test/Azure.Functions.Cli.Tests/E2E/StartTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,76 @@ await CliTester.Run(new RunConfiguration[]
145145
}, _output);
146146
}
147147

148+
[Fact]
149+
public async Task start_displays_error_on_invalid_host_json()
150+
{
151+
var functionName = "HttpTriggerCSharp";
152+
153+
await CliTester.Run(new RunConfiguration[]
154+
{
155+
new RunConfiguration
156+
{
157+
Commands = new[]
158+
{
159+
"init . --worker-runtime dotnet",
160+
$"new --template Httptrigger --name {functionName}",
161+
162+
},
163+
Test = async (workingDir, p) =>
164+
{
165+
var filePath = Path.Combine(workingDir, "host.json");
166+
string hostJsonContent = "{ \"version\": \"2.0\", \"extensionBundle\": { \"id\": \"Microsoft.Azure.Functions.ExtensionBundle\", \"version\": \"[1.*, 2.0.0)\" }}";
167+
await File.WriteAllTextAsync(filePath, hostJsonContent);
168+
},
169+
},
170+
new RunConfiguration
171+
{
172+
Commands = new[]
173+
{
174+
"start"
175+
},
176+
ExpectExit = true,
177+
ExitInError = true,
178+
ErrorContains = new[] { "Extension bundle configuration should not be present" },
179+
},
180+
}, _output, startHost: true);
181+
}
182+
183+
184+
[Fact]
185+
public async Task start_displays_error_on_missing_host_json()
186+
{
187+
var functionName = "HttpTriggerCSharp";
188+
189+
await CliTester.Run(new RunConfiguration[]
190+
{
191+
new RunConfiguration
192+
{
193+
Commands = new[]
194+
{
195+
"init . --worker-runtime dotnet",
196+
$"new --template Httptrigger --name {functionName}",
197+
},
198+
Test = async (workingDir, p) =>
199+
{
200+
var hostJsonPath = Path.Combine(workingDir, "host.json");
201+
File.Delete(hostJsonPath);
202+
203+
},
204+
},
205+
new RunConfiguration
206+
{
207+
Commands = new[]
208+
{
209+
"start"
210+
},
211+
ExpectExit = true,
212+
ExitInError = true,
213+
ErrorContains = new[] { "Host.json file in missing" },
214+
},
215+
}, _output);
216+
}
217+
148218
[Fact]
149219
public async Task start_host_port_in_use()
150220
{

0 commit comments

Comments
 (0)