Skip to content

Commit 27ca25f

Browse files
committed
Use empty array instead of uninitialized array
Enumerating uninitialized array will throw InvalidOperationException
1 parent 7aa2486 commit 27ca25f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/WebJobs.Script/Config/ScriptJobHostOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public string RootScriptPath
3030
get => _rootScriptPath;
3131
set
3232
{
33-
_directorySnapshot = default;
33+
_directorySnapshot = ImmutableArray<string>.Empty;
3434
_rootScriptPath = value;
3535
}
3636
}
@@ -39,7 +39,7 @@ public ImmutableArray<string> RootScriptDirectorySnapshot
3939
{
4040
get
4141
{
42-
if (_rootScriptPath != null && _directorySnapshot.IsDefault)
42+
if (_rootScriptPath != null && _directorySnapshot.IsDefaultOrEmpty)
4343
{
4444
// take a startup time function directory snapshot so we can detect function additions/removals
4545
// we'll also use this snapshot when reading function metadata as part of startup
@@ -51,7 +51,7 @@ public ImmutableArray<string> RootScriptDirectorySnapshot
5151
}
5252
catch (DirectoryNotFoundException)
5353
{
54-
_directorySnapshot = default;
54+
_directorySnapshot = ImmutableArray<string>.Empty;
5555
}
5656
}
5757

test/WebJobs.Script.Tests/FunctionMetadataManagerTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.IO.Abstractions.TestingHelpers;
8+
using Microsoft.Azure.WebJobs.Script.Rpc;
89
using Microsoft.Extensions.Logging.Abstractions;
10+
using Microsoft.Extensions.Options;
911
using Newtonsoft.Json.Linq;
1012
using Xunit;
1113

@@ -36,6 +38,24 @@ public void DeterminePrimaryScriptFile_MultipleFiles_SourceFileSpecified(string
3638
Assert.Equal(@"c:\functions\queueTrigger.py", scriptFile, StringComparer.OrdinalIgnoreCase);
3739
}
3840

41+
[Theory]
42+
[InlineData(@"NonExistentPath")]
43+
[InlineData(null)]
44+
public void InitializesEmptyOrMissingDirectorySnapshot(string rootScriptPath)
45+
{
46+
var scriptConfig = new ScriptJobHostOptions()
47+
{
48+
RootScriptPath = rootScriptPath
49+
};
50+
51+
IOptions<ScriptJobHostOptions> scriptOptions = new OptionsManager<ScriptJobHostOptions>(new TestOptionsFactory<ScriptJobHostOptions>(scriptConfig));
52+
IOptions<LanguageWorkerOptions> languageWorkerOptions = new OptionsManager<LanguageWorkerOptions>(new TestOptionsFactory<LanguageWorkerOptions>(new LanguageWorkerOptions()));
53+
54+
var functionMetadataManager = new FunctionMetadataManager(scriptOptions, languageWorkerOptions, NullLoggerFactory.Instance);
55+
Assert.False(functionMetadataManager.Functions.IsDefault);
56+
Assert.True(functionMetadataManager.Functions.IsEmpty);
57+
}
58+
3959
[Fact]
4060
public void DeterminePrimaryScriptFile_RelativeSourceFileSpecified()
4161
{

0 commit comments

Comments
 (0)