Skip to content

Commit

Permalink
Updating setup-dapr with latest dependencies and practices for gh jav…
Browse files Browse the repository at this point in the history
…ascript actions (#142)

* Updating Node and javascript dependencies to latest LTS using Node v20

Signed-off-by: Paul Yuknewicz <[email protected]>

* Updating readme and CI/CD tests to reflect Dapr latest is 1.13.0, not 1.0.0

Signed-off-by: Paul Yuknewicz <[email protected]>

* Fixed test* and coverage* run tasks

Signed-off-by: Paul Yuknewicz <[email protected]>

* Configured eslint

Signed-off-by: Paul Yuknewicz <[email protected]>

* Fixing eslint errors

Signed-off-by: Paul Yuknewicz <[email protected]>

* Fixed ESLint errors for expect(error) asserts

Signed-off-by: Paul Yuknewicz <[email protected]>

* Updating package and dist files now that npm run test and npm run coverage work

Signed-off-by: Paul Yuknewicz <[email protected]>

* Adding built in support for dapr init

Signed-off-by: Paul Yuknewicz <[email protected]>

* Add workflow step to initialize Dapr; use environment variables

Signed-off-by: Paul Yuknewicz <[email protected]>

* Updating action to exclude dapr init for Mac (no docker support)

Signed-off-by: Paul Yuknewicz <[email protected]>

* Dapr init only in Ubuntu

Signed-off-by: Paul Yuknewicz <[email protected]>

* Reverting parameter name to `version` to avoid breaking changes

Signed-off-by: Paul Yuknewicz <[email protected]>

* Clarifying readme

Signed-off-by: Paul Yuknewicz <[email protected]>

* Update global.env

Signed-off-by: Artur Souza <[email protected]>

---------

Signed-off-by: Paul Yuknewicz <[email protected]>
Signed-off-by: Artur Souza <[email protected]>
Co-authored-by: Artur Souza <[email protected]>
  • Loading branch information
paulyuk and artursouza authored Jul 1, 2024
1 parent 45e34f2 commit 8d98091
Show file tree
Hide file tree
Showing 23 changed files with 43,374 additions and 9,787 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
lib/
dist/
node_modules/
coverage/
17 changes: 0 additions & 17 deletions .eslintrc.json

This file was deleted.

5 changes: 5 additions & 0 deletions .github/codeql/codeql-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: JavaScript CodeQL Configuration

paths-ignore:
- node_modules
- dist
3 changes: 3 additions & 0 deletions .github/env/global.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DAPR_CLI_VERSION: 1.13.0
DAPR_RUNTIME_VERSION: 1.13.5
DAPR_DEFAULT_IMAGE_REGISTRY: ghcr
50 changes: 50 additions & 0 deletions .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
env:
commonjs: true
es6: true
jest: true
node: true

globals:
Atomics: readonly
SharedArrayBuffer: readonly

ignorePatterns:
- "!.*"
- "**/node_modules/.*"
- "**/dist/.*"
- "**/coverage/.*"
- "*.json"

parser: "@babel/eslint-parser"

parserOptions:
ecmaVersion: 2023
sourceType: module
requireConfigFile: false
babelOptions:
babelrc: false
configFile: false
presets:
- jest

plugins:
- jest

extends:
- eslint:recommended
- plugin:github/recommended
- plugin:jest/recommended

rules:
{
"camelcase": "off",
"eslint-comments/no-use": "off",
"eslint-comments/no-unused-disable": "off",
"i18n-text/no-en": "off",
"import/no-commonjs": "off",
"import/no-namespace": "off",
"no-console": "off",
"no-unused-vars": "off",
"prettier/prettier": "error",
"semi": "off",
}
18 changes: 18 additions & 0 deletions .github/linters/.markdown-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Unordered list style
MD004:
style: dash

# Ordered list item prefix
MD029:
style: one

# Spaces after list markers
MD030:
ul_single: 1
ol_single: 1
ul_multi: 1
ol_multi: 1

# Code block style
MD046:
style: fenced
10 changes: 10 additions & 0 deletions .github/linters/.yaml-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules:
document-end: disable
document-start:
level: warning
present: false
line-length:
level: warning
max: 80
allow-non-breakable-words: true
allow-non-breakable-inline-mappings: true
41 changes: 25 additions & 16 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
units:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm ci
- run: npm test
- uses: actions/checkout@v2
- run: npm ci
- run: npm test

# test action works running from the graph
test:
Expand All @@ -21,16 +21,25 @@ jobs:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v2
# Test that the default version is used
- name: Install default version
uses: ./
if: ${{ matrix.os == 'windows-latest'}}
# Test that explicit version is used
- name: Install specific version
uses: ./
if: ${{ matrix.os != 'windows-latest'}}
with:
version: 1.0.0
- name: Run Dapr
run: dapr --version
- uses: actions/checkout@v2
- name: Load environment variables
uses: artursouza/export-env-action@v2
with:
envFile: "./.github/env/global.env"
expand: "true"
# Test that the default version is used
- name: Setup Dapr CLI default version
uses: ./
if: ${{ matrix.os == 'windows-latest'}}
# Test that explicit version is used
- name: Setup Dapr CLI specific version
uses: ./
if: ${{ matrix.os != 'windows-latest'}}
with:
version: ${{ env.DAPR_CLI_VERSION }}
- name: Initialize Dapr
if: ${{ matrix.os == 'ubuntu-latest'}}
run: |
export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
dapr init --runtime-version=${{ env.DAPR_RUNTIME_VERSION }}
dapr --version
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@

## Install a specific version of Dapr CLI on a GitHub Actions runner

Acceptable values are any semantic version string like 1.2.0. Use this action in workflow to define which version of Dapr will be used.
Acceptable values are any semantic version string like 1.13.0. Use this action in workflow to define which version of Dapr CLI will be used.

```yaml
- uses: dapr/setup-dapr@v1
- name: Setup Dapr CLI specific version
uses: dapr/setup-dapr@v1
with:
version: '<version>' # default is 1.2.0
version: "1.13.0"
id: install
```
The cached Dapr CLI path is added to the PATH environment variable as well as stored in the dapr-path output variable.
Refer to the action metadata file for details about all the inputs <https://github.com/dapr/setup-dapr/blob/main/action.yml>
This action does not initialize the Dapr Runtime. To do this add another step to initialize like this and remember that Dapr CLI and Dapr Runtime versions may differ:
```yaml
- name: Initialize Dapr
if: ${{ matrix.os == 'ubuntu-latest'}}
run: |
export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
dapr init --runtime-version=1.13.0
dapr --version
```
For a complete example see [./.github/workflows/test.yaml](./.github/workflows/test.yml)
## Code of Conduct
Please refer to our [Dapr Community Code of Conduct](https://github.com/dapr/community/blob/master/CODE-OF-CONDUCT.md)
101 changes: 54 additions & 47 deletions index.test.js → __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const fs = require("fs");
const path = require("path");
const action = require("./action");
const action = require("../src/action");
const tc = require("@actions/tool-cache");

describe("run", () => {
test("should unzip Windows zip", async () => {
// Arrange
jest.spyOn(tc, "find").mockReturnValue();
jest.spyOn(tc, "cacheDir").mockReturnValue("/dapr/");
let extractZip = jest.spyOn(tc, "extractZip").mockReturnValue();
const extractZip = jest.spyOn(tc, "extractZip").mockReturnValue();
jest.spyOn(tc, "downloadTool").mockReturnValue("dapr_darwin_amd64.tar.gz");

jest.spyOn(fs, "chmodSync").mockReturnValue();
Expand All @@ -33,31 +33,31 @@ describe("run", () => {
describe("getDaprDownloadURL", () => {
test("should download the Windows version", () => {
// Act
let url = action.getDaprDownloadURL("Windows_NT", "1.0.0");
const url = action.getDaprDownloadURL("Windows_NT", "1.0.0");

// Assert
expect(url).toEqual(
"https://github.com/dapr/cli/releases/download/v1.0.0/dapr_windows_amd64.zip"
"https://github.com/dapr/cli/releases/download/v1.0.0/dapr_windows_amd64.zip",
);
});

test("should download the Linux version", () => {
// Act
let url = action.getDaprDownloadURL("Linux", "1.0.0");
const url = action.getDaprDownloadURL("Linux", "1.0.0");

// Assert
expect(url).toEqual(
"https://github.com/dapr/cli/releases/download/v1.0.0/dapr_linux_amd64.tar.gz"
"https://github.com/dapr/cli/releases/download/v1.0.0/dapr_linux_amd64.tar.gz",
);
});

test("should download the Darwin version", () => {
// Act
let url = action.getDaprDownloadURL("Darwin", "1.0.0");
const url = action.getDaprDownloadURL("Darwin", "1.0.0");

// Assert
expect(url).toEqual(
"https://github.com/dapr/cli/releases/download/v1.0.0/dapr_darwin_amd64.tar.gz"
"https://github.com/dapr/cli/releases/download/v1.0.0/dapr_darwin_amd64.tar.gz",
);
});
});
Expand All @@ -78,7 +78,7 @@ describe("downloadDapr", () => {
});

// Act
let actual = await action.downloadDapr("Windows_NT", "1.1.0");
const actual = await action.downloadDapr("Windows_NT", "1.1.0");

// Assert
// Restore mocks so the testing framework can use the fs functions
Expand All @@ -94,7 +94,7 @@ describe("downloadDapr", () => {

jest.spyOn(tc, "find").mockReturnValue("/dapr/");
jest.spyOn(tc, "cacheDir").mockReturnValue("/dapr/");
let extractTar = jest.spyOn(tc, "extractTar").mockReturnValue();
const extractTar = jest.spyOn(tc, "extractTar").mockReturnValue();
jest.spyOn(tc, "downloadTool").mockReturnValue("dapr_darwin_amd64.tar.gz");

jest.spyOn(path, "join").mockReturnValue("");
Expand All @@ -118,23 +118,26 @@ describe("downloadDapr", () => {
},
});

let error;
try {
// Act
await action.downloadDapr("Darwin", "1.0.0");
} catch (error) {
// Restore mocks so the testing framework can use the fs functions
jest.restoreAllMocks();

// Assert
expect(error).toBeInstanceOf(Error);
expect(error).toHaveProperty(
"message",
"Dapr executable not found in path /dapr/"
);

// Number of calls should be zero because of the error
expect(extractTar).toHaveBeenCalledTimes(0);
} catch (err) {
error = err;
}

// Restore mocks so the testing framework can use the fs functions
jest.restoreAllMocks();

// Assert
expect(error).toBeInstanceOf(Error);
expect(error).toHaveProperty(
"message",
"Dapr executable not found in path /dapr/",
);

// Number of calls should be zero because of the error
expect(extractTar).toHaveBeenCalledTimes(0);
});

test("should throw if fileList is empty", async () => {
Expand All @@ -143,28 +146,30 @@ describe("downloadDapr", () => {

jest.spyOn(tc, "find").mockReturnValue();
jest.spyOn(tc, "cacheDir").mockReturnValue("/dapr/");
let extractTar = jest.spyOn(tc, "extractTar").mockReturnValue();
const extractTar = jest.spyOn(tc, "extractTar").mockReturnValue();
jest.spyOn(tc, "downloadTool").mockReturnValue("dapr_darwin_amd64.tar.gz");

jest.spyOn(fs, "chmodSync").mockReturnValue();
jest.spyOn(fs, "readdirSync").mockReturnValue([]);

let error;
try {
// Act
await action.downloadDapr("Darwin", "1.0.0");
} catch (error) {
// Restore mocks so the testing framework can use the fs functions
jest.restoreAllMocks();

// Assert
expect(error).toBeInstanceOf(Error);
expect(error).toHaveProperty(
"message",
"Dapr executable not found in path /dapr/"
);

expect(extractTar).toHaveBeenCalledTimes(1);
} catch (err) {
error = err;
}
// Restore mocks so the testing framework can use the fs functions
jest.restoreAllMocks();

// Assert
expect(error).toBeInstanceOf(Error);
expect(error).toHaveProperty(
"message",
"Dapr executable not found in path /dapr/",
);

expect(extractTar).toHaveBeenCalledTimes(1);
});

test("should throw on download failure", async () => {
Expand All @@ -173,22 +178,24 @@ describe("downloadDapr", () => {

jest.spyOn(tc, "find").mockReturnValue();
jest.spyOn(tc, "downloadTool").mockImplementation(() => {
throw "error";
throw new Error("error");
});

let error;
try {
// Act
await action.downloadDapr("Darwin", "1.0.0");
} catch (error) {
// Restore mocks so the testing framework can use the fs functions
jest.restoreAllMocks();

// Assert
expect(error).toBeInstanceOf(Error);
expect(error).toHaveProperty(
"message",
"Failed to download Dapr from location https://github.com/dapr/cli/releases/download/v1.0.0/dapr_darwin_amd64.tar.gz"
);
} catch (err) {
error = err;
}
// Restore mocks so the testing framework can use the fs functions
jest.restoreAllMocks();

// Assert
expect(error).toBeInstanceOf(Error);
expect(error).toHaveProperty(
"message",
"Failed to download Dapr from location https://github.com/dapr/cli/releases/download/v1.0.0/dapr_darwin_amd64.tar.gz",
);
});
});
Loading

0 comments on commit 8d98091

Please sign in to comment.