Use github actions for test and build
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a1a9897 --- /dev/null +++ b/.github/workflows/ci.yml
@@ -0,0 +1,41 @@ +name: CI + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + test-build: + runs-on: ubuntu-latest + + services: + pubsub: + image: knarz/pubsub-emulator:latest + env: + PUBSUB_EMULATOR_HOST: localhost:8085 + PUBSUB_PROJECT_ID: casbin + ports: + - 8085:8085 + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup Node.js + uses: actions/setup-node@v1 + with: + node-version: 12 + + - name: Install dependencies + run: npm ci + + - name: test + run: npm test + env: + PUBSUB_EMULATOR_HOST: localhost:8085 + PUBSUB_PROJECT_ID: casbin + + - name: build + run: npm run build --if-present
diff --git a/README.md b/README.md index b38ba5e..6c0d4eb 100644 --- a/README.md +++ b/README.md
@@ -1,6 +1,7 @@ # pubsub-watcher [![NPM version][npm-image]][npm-url] + [npm-image]: https://img.shields.io/npm/v/@casbin/pubsub-watcher.svg?style=flat-square [npm-url]: https://npmjs.org/package/@casbin/pubsub-watcher @@ -41,3 +42,10 @@ # Test Set `GOOGLE_APPLICATION_CREDENTIALS` in environment variable and `npm run test` + +Test uses [Pub/Sub emulator](https://cloud.google.com/pubsub/docs/emulator) + +```sh +gcloud beta emulators pubsub start --project=casbin +PUBSUB_EMULATOR_HOST=localhost:8085 PUBSUB_PROJECT_ID=casbin npm run test +```
diff --git a/package-lock.json b/package-lock.json index 186ae8d..428df78 100644 --- a/package-lock.json +++ b/package-lock.json
@@ -1,6 +1,6 @@ { "name": "@casbin/pubsub-watcher", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 1, "requires": true, "dependencies": {
diff --git a/test/watcher.test.ts b/test/watcher.test.ts index 78f870b..08f1c57 100644 --- a/test/watcher.test.ts +++ b/test/watcher.test.ts
@@ -13,19 +13,40 @@ // limitations under the License. import { newEnforcer } from 'casbin'; +import { PubSub } from '@google-cloud/pubsub'; import { PubsubWatcher } from '../src/watcher'; describe('Test', () => { + const projectId = 'casbin'; + const topicName = 'casbin'; + const subscriptionName1 = 'sub_casbin_1'; + const subscriptionName2 = 'sub_casbin_2'; + let watcher: PubsubWatcher; let updater: PubsubWatcher; + beforeAll(async () => { + const pubsub = new PubSub({ projectId }); + const [topic] = await pubsub.createTopic(topicName); + console.log(`Topic ${topic.name} created.`); + await pubsub.topic(topicName).createSubscription(subscriptionName1); + await pubsub.topic(topicName).createSubscription(subscriptionName2); + }); + + afterAll(async () => { + const pubsub = new PubSub({ projectId }); + await pubsub.topic(topicName).delete(); + await pubsub.subscription(subscriptionName1).delete(); + await pubsub.subscription(subscriptionName2).delete(); + }); + afterEach(async () => { if (watcher) await watcher.close(); if (updater) await updater.close(); }); test('Test1', async (done) => { - watcher = await PubsubWatcher.newWatcher(undefined, 'casbin', 'sub_casbin_1'); + watcher = await PubsubWatcher.newWatcher({ projectId }, topicName, subscriptionName1); const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv'); enforcer.setWatcher(watcher); watcher.setUpdateCallback(done); @@ -33,12 +54,14 @@ }); test('Test2', async (done) => { - watcher = await PubsubWatcher.newWatcher(undefined, 'casbin', 'sub_casbin_1'); + watcher = await PubsubWatcher.newWatcher({ projectId }, topicName, subscriptionName1); const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv'); enforcer.setWatcher(watcher); watcher.setUpdateCallback(done); - updater = await PubsubWatcher.newWatcher(undefined, 'casbin', 'sub_casbin_2'); + updater = await PubsubWatcher.newWatcher({ projectId }, topicName, subscriptionName2); await updater.update(); }); + + afterAll(async () => {}); });