Skip to content

Commit 002044e

Browse files
mikedingjanmikedebocktbarlow12
authored
fix: Check for valid index before setting name property (serverless#480)
* fix: Check for valid index before setting name property This commit fixes a bug when using the timer trigger where it tries to set the name of the binding. The name of the binding can't be set since there is no index found (-1) Fixes: serverless#444 * added unittest for valid index in createBindings Co-authored-by: Mike de Bock <mike.debock@masterwebber.nl> Co-authored-by: Tanner Barlow <tanner.barlow.dev@gmail.com>
1 parent 82ef10e commit 002044e

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/services/packageService.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Serverless from "serverless";
55
import { FunctionMetadata } from "../shared/utils";
66
import { MockFactory } from "../test/mockFactory";
77
import { PackageService } from "./packageService";
8+
import { Runtime } from "../config/runtime";
89

910
jest.mock("rimraf");
1011
import rimraf from "rimraf";
@@ -228,6 +229,32 @@ describe("Package Service", () => {
228229
writeFileSpy.mockRestore();
229230
});
230231

232+
it("createBinding should be able to create function.json without bindings", async () => {
233+
// Create service provider with python 3.8 runtime
234+
const provider = MockFactory.createTestAzureServiceProvider()
235+
provider.runtime = Runtime.PYTHON38
236+
sls = MockFactory.createTestServerless({"service": { "provider": provider }});
237+
sls.config.servicePath = process.cwd();
238+
sls.service["functions"] = {
239+
hello: MockFactory.createTestAzureFunctionConfig(functionRoute),
240+
eventhubHandler: MockFactory.createTestEventHubFunctionConfig(),
241+
}
242+
243+
const functionName = "hello";
244+
const functionMetadata: FunctionMetadata = {
245+
entryPoint: "handler",
246+
handlerPath: "src/handlers/hello",
247+
params: {
248+
functionJson: {
249+
bindings: []
250+
},
251+
},
252+
};
253+
254+
packageService = new PackageService(sls, MockFactory.createTestServerlessOptions());
255+
await packageService.createBinding(functionName, functionMetadata);
256+
});
257+
231258
it("webpack copies required", async () => {
232259
mockFs({
233260
// Generated by webpack plugin

src/services/packageService.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class PackageService extends BaseService {
108108
rimraf.sync(folderPath);
109109
}
110110
});
111-
111+
112112
// Delete function folder if empty
113113
const items = fs.readdirSync(functionName);
114114
if (items.length === 0) {
@@ -122,7 +122,7 @@ export class PackageService extends BaseService {
122122
fs.unlinkSync(filePath);
123123
}
124124
}
125-
125+
126126
for (const dir of rootFoldersToRemove) {
127127
const dirPath = path.join(this.serverless.config.servicePath, dir);
128128
if (fs.existsSync(dirPath)) {
@@ -151,22 +151,26 @@ export class PackageService extends BaseService {
151151
if (this.configService.isPythonTarget()) {
152152
const index = (functionJSON.bindings as any[])
153153
.findIndex((binding) => (!binding.direction || binding.direction === "out"));
154-
functionJSON.bindings[index].name = "$return";
155-
} else {
154+
155+
if (functionJSON.bindings[index]) {
156+
functionJSON.bindings[index].name = "$return";
157+
}
156158
}
157159
functionJSON.scriptFile = handlerPath;
158160

159161
const functionObject = this.configService.getFunctionConfig()[functionName];
160162
const incomingBinding = Utils.getIncomingBindingConfig(functionObject);
161-
163+
162164
const bindingAzureSettings = Utils.get(incomingBinding, constants.xAzureSettings, incomingBinding);
163165

164166
if (bindingAzureSettings.route) {
165167
// Find incoming binding within functionJSON and set the route
166168
const index = (functionJSON.bindings as any[])
167169
.findIndex((binding) => (!binding.direction || binding.direction === "in"));
168170

169-
functionJSON.bindings[index].route = bindingAzureSettings.route;
171+
if (functionJSON.bindings[index]) {
172+
functionJSON.bindings[index].route = bindingAzureSettings.route;
173+
}
170174
}
171175

172176
return functionJSON;

0 commit comments

Comments
 (0)