-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintegration.test.js
136 lines (122 loc) · 5.39 KB
/
integration.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { exec, fork } from 'child_process'
import { tmpdir } from 'os'
import { resolve as pathResolve } from 'path'
import { readFile } from 'fs-extra'
import { v4 as uuidv4 } from 'uuid'
import { minimize, setupContext } from '../src'
describe('integration', () => {
test('should install globally', async () => {
const result = await new Promise((resolve, reject) => {
exec('npm i -g', { cwd: pathResolve(__dirname, '..') }, (error) => {
if (error) {
return reject(error)
}
return resolve(true)
})
})
expect(result).toEqual(true)
}, 30000)
test('require index.module imports without error', () => {
expect(() => {
require('../index.module')
}).not.toThrow()
})
test('programmatic minimize file', async () => {
const context = setupContext({
logger: console
})
const result = await minimize(context, {
filePath: pathResolve(__dirname, 'files', 'firestore.rules')
})
expect(result).toEqual(
"rules_version='2';service cloud.firestore{match/databases/{database}/documents{function someFunc(someParam1,someParam2){let foo=true;return someParam1.keys().hasAll([someParam2])&&foo;}match/some/path/{arg1}/{arg2}{allow read:if someFunc(arg1,arg2);allow create:if someFunc(arg1,arg2);allow update:if someFunc(arg1,arg2);allow delete:if someFunc(arg1,arg2);}}}"
)
})
test('programmatic minimize file and output to another file', async () => {
const context = setupContext({
logger: console
})
const output = pathResolve(tmpdir(), uuidv4(), 'firestore.min.rules')
const returned = await minimize(context, {
filePath: pathResolve(__dirname, 'files', 'firestore.rules'),
output
})
const result = await readFile(output, 'utf-8')
expect(returned).toEqual(undefined)
expect(result).toEqual(
"rules_version='2';service cloud.firestore{match/databases/{database}/documents{function someFunc(someParam1,someParam2){let foo=true;return someParam1.keys().hasAll([someParam2])&&foo;}match/some/path/{arg1}/{arg2}{allow read:if someFunc(arg1,arg2);allow create:if someFunc(arg1,arg2);allow update:if someFunc(arg1,arg2);allow delete:if someFunc(arg1,arg2);}}}"
)
})
test('binary minimize file and output to stdout', async () => {
// NOTE BRN: This runs against the production built code so you must `npm run build` before this
// will pass.
const result = await new Promise((resolve, reject) => {
let output = ''
let errorOccured
const child = fork(
pathResolve(__dirname, '..', 'bin', 'firemin'),
['minimize', '-f', pathResolve(__dirname, 'files', 'firestore.rules')],
{ cwd: pathResolve(__dirname, '..'), stdio: 'pipe' }
)
child.on('error', (error) => {
errorOccured = error
})
child.stdout.on('data', (data) => {
output += data
})
child.on('exit', () => {
if (errorOccured) {
return reject(errorOccured)
}
resolve(output)
})
})
expect(result).toEqual(
"rules_version='2';service cloud.firestore{match/databases/{database}/documents{function someFunc(someParam1,someParam2){let foo=true;return someParam1.keys().hasAll([someParam2])&&foo;}match/some/path/{arg1}/{arg2}{allow read:if someFunc(arg1,arg2);allow create:if someFunc(arg1,arg2);allow update:if someFunc(arg1,arg2);allow delete:if someFunc(arg1,arg2);}}}"
)
})
test('minifier removes unused functions', async () => {
const context = setupContext({
logger: console
})
const result = await minimize(context, {
filePath: pathResolve(__dirname, 'files', 'firestore.unused-functions.rules')
})
expect(result).toEqual(
"rules_version='2';service cloud.firestore{match/databases/{database}/documents{match/some/path/{arg1}/{arg2}{allow read:if arg1.keys().hasAll([arg2]);allow create:if arg1.keys().hasAll([arg2]);allow update:if arg1.keys().hasAll([arg2]);allow delete:if arg1.keys().hasAll([arg2]);}}}"
)
})
test('minfier collapses single use functions', async () => {
const context = setupContext({
logger: console
})
const result = await minimize(context, {
filePath: pathResolve(__dirname, 'files', 'firestore.single-use-functions.rules')
})
expect(result).toEqual(
"rules_version='2';service cloud.firestore{match/databases/{database}/documents{match/some/path/{arg1}/{arg2}{allow read:if(arg1&&arg2);allow create:if!arg1;}}}"
)
})
test('collapses nested property access with same name correctly', async () => {
const context = setupContext({
logger: console
})
const result = await minimize(context, {
filePath: pathResolve(__dirname, 'files', 'firestore.nested-property-access.rules')
})
expect(result).toEqual(
"rules_version='2';service cloud.firestore{match/databases/{database}/documents{match/some/path/{arg}{allow read:if arg.data.data;}}}"
)
})
test('minfier collapses small functions', async () => {
const context = setupContext({
logger: console
})
const result = await minimize(context, {
filePath: pathResolve(__dirname, 'files', 'firestore.small-functions.rules')
})
expect(result).toEqual(
"rules_version='2';service cloud.firestore{match/databases/{database}/documents{match/some/path/{arg1}/{arg2}{allow read:if!arg1;allow create:if!arg1;allow update:if(arg1&&arg2);allow delete:if(arg1&&arg2);}}}"
)
})
})