Skip to content

Commit

Permalink
fix: Fix failing invoke with data path (#321)
Browse files Browse the repository at this point in the history
Moves data path initialization outside of the constructor and does not replace the `data` property within options. Instead, passes the contents of the file path to the invoke service.

Fixes #320
  • Loading branch information
tbarlow12 committed Sep 13, 2019
1 parent d588206 commit bad7146
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,19 @@ $ sls invoke -f <functionName>
After deploying template function app, run

```bash
$ sls invoke -f hello '{"name": "Azure"}'
$ sls invoke -f hello -d '{"name": "Azure"}'
```

If you have a JSON object in a file, you could run

```bash
$ sls invoke -f hello -p data.json
```

If you have your service running locally (in another terminal), you can run:

```bash
$ sls invoke local -f hello -p data.json
```

### Roll Back Your Function App
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/invoke/azureInvokePlugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,29 @@ describe("Azure Invoke Plugin", () => {
options["function"] = "testApp";
options["path"] = "testFile.json";
options["method"] = "GET";
const plugin = new AzureInvokePlugin(sls, options);
const plugin = new AzureInvokePlugin(sls, {
function: "testApp",
path: "testFile.json",
} as any);
await invokeHook(plugin, "invoke:invoke");
expect(invoke).toBeCalledWith(options["method"], options["function"], fileContent);
expect(sls.cli.log).toBeCalledWith(JSON.stringify(expectedResult.data));

});

it("calls the invoke hook with file path", async () => {
it("fails when data file path does not exist", async () => {
const invoke = jest.fn();
InvokeService.prototype.invoke = invoke;
const sls = MockFactory.createTestServerless();
const options = MockFactory.createTestServerlessOptions();
options["function"] = "testApp";
options["path"] = "notExist.json";
options["method"] = "GET";
expect(() => new AzureInvokePlugin(sls, options)).toThrow();
const plugin = new AzureInvokePlugin(sls, {
function: "testApp",
path: "notExist.json",
} as any);
await expect(invokeHook(plugin, "invoke:invoke")).rejects.toThrow();
});

it("Function invoked with no data", async () => {
Expand Down
30 changes: 14 additions & 16 deletions src/plugins/invoke/azureInvokePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,12 @@ import { isAbsolute, join } from "path";
import Serverless from "serverless";
import { InvokeService } from "../../services/invokeService";
import { AzureBasePlugin } from "../azureBasePlugin";
import path from "path";

export class AzureInvokePlugin extends AzureBasePlugin {

public constructor(serverless: Serverless, options: Serverless.Options) {
super(serverless, options);
const path = this.options["path"];

if (path) {
const absolutePath = isAbsolute(path)
? path
: join(this.serverless.config.servicePath, path);
this.log(this.serverless.config.servicePath);
this.log(path);

if (!fs.existsSync(absolutePath)) {
throw new Error("The file you provided does not exist.");
}
this.options["data"] = fs.readFileSync(absolutePath).toString();
}

this.commands = {
invoke: {
Expand Down Expand Up @@ -108,17 +95,28 @@ export class AzureInvokePlugin extends AzureBasePlugin {

private async invoke(local: boolean = false) {
const functionName = this.options["function"];
const data = this.options["data"];
const method = this.options["method"] || "GET";
if (!functionName) {
this.log("Need to provide a name of function to invoke");
return;
}

const invokeService = new InvokeService(this.serverless, this.options, local);
const response = await invokeService.invoke(method, functionName, data);
const response = await invokeService.invoke(method, functionName, this.getData());
if (response) {
this.log(JSON.stringify(response.data));
}
}

private getData() {
let dataPath = this.getOption("path");
if (dataPath) {
dataPath = (isAbsolute(dataPath)) ? dataPath : path.join(this.serverless.config.servicePath, dataPath);
if (!fs.existsSync(dataPath)) {
throw new Error("The file you provided does not exist.");
}
return fs.readFileSync(dataPath).toString();
}
return this.getOption("data");
}
}

0 comments on commit bad7146

Please sign in to comment.