-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprune.spec.ts
172 lines (144 loc) · 5.23 KB
/
prune.spec.ts
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { FilterState, base64UrlEncode, deserializeFilter, serializeFilter } from '@appland/models';
import fs from 'fs';
import fse from 'fs-extra';
import path from 'path';
import sinon from 'sinon';
import tmp from 'tmp';
import yargs from 'yargs';
import { fromFile } from '../../src/cmds/prune/prune';
import * as PruneAppMap from '../../src/cmds/prune/pruneAppMap';
import { readFile } from 'fs/promises';
const fixtureDir = path.join(__dirname, 'fixtures', 'ruby');
const cmd = require('../../src/cmds/prune/prune').default;
tmp.setGracefulCleanup();
const statsMapFile = path.join(
__dirname,
'fixtures',
'stats',
'appmap',
'Microposts_interface_micropost_interface.appmap.json'
);
describe('prune subcommand', () => {
let projectDir: string;
beforeEach(() => {
projectDir = tmp.dirSync({} as any).name;
fse.copySync(fixtureDir, projectDir);
});
afterEach(() => {
sinon.restore();
});
it('works', async () => {
const parser = yargs.command(cmd);
sinon
.stub(PruneAppMap, 'pruneAppMap')
.returns({ appmap: { events: [], data: { pruneFilter: [] } } });
const appMapFile = path.join(projectDir, 'revoke_api_key.appmap.json');
const cmdLine = `prune -o ${projectDir} ${appMapFile} 2MB`;
await new Promise((resolve, reject) => {
parser.parse(cmdLine, {}, (err, argv, output) => {
if (err) {
reject(err);
} else {
resolve(output);
}
});
});
// Check that we wrote the output file correctly. Validity of pruning is
// tested in @appland/models.
const actual = await fromFile(appMapFile);
expect(JSON.stringify(actual)).toEqual('{"events":[],"data":{"pruneFilter":[]}}');
});
it('correctly reduces the size of an appmap from a base64url-encoded filter', async () => {
const filterState = {
hideName: [
'function:logger/Logger::LogDevice#write',
'function:activerecord/ActiveRecord::Relation#records',
'function:actionpack/ActionDispatch::Request::Session#[]',
],
limitRootEvents: false,
hideMediaRequests: false,
} as FilterState;
const serializedFilterState = base64UrlEncode(JSON.stringify(filterState));
const baseAppMap = JSON.parse(await readFile(statsMapFile, 'utf8'));
expect(baseAppMap.events.length).toEqual(3526);
const argv = {
file: statsMapFile,
filter: serializedFilterState,
outputDir: projectDir,
};
const pruneResult = await cmd.handler(argv);
const outPath = path.join(projectDir, 'Microposts_interface_micropost_interface.appmap.json');
const appmap = JSON.parse(await readFile(outPath, 'utf8'));
expect(appmap.events.length).toEqual(2184);
expect(serializeFilter(pruneResult.filter)).toEqual(filterState);
});
it('correctly combines pruneFilter data', async () => {
const statsMapData = (await fromFile(statsMapFile)) as any;
statsMapData.pruneFilter = {
hideName: ['fakeNameOne', 'fakeNameTwo'],
hideUnlabeled: true,
hideElapsedTimeUnder: '10',
};
const newFilter = {
hideName: [
'function:logger/Logger::LogDevice#write',
'function:activerecord/ActiveRecord::Relation#records',
'function:actionpack/ActionDispatch::Request::Session#[]',
],
hideElapsedTimeUnder: 50, // Numbers are accepted as well
hideMediaRequests: true,
};
const prunedMapData = PruneAppMap.pruneWithFilter(
statsMapData,
base64UrlEncode(JSON.stringify(newFilter))
);
const expectedFilter = {
hideElapsedTimeUnder: 10, // The lower of the two values
// hideMediaRequests: true, // Omitted because it's on by default
hideName: [
'fakeNameOne',
'fakeNameTwo',
'function:actionpack/ActionDispatch::Request::Session#[]',
'function:activerecord/ActiveRecord::Relation#records',
'function:logger/Logger::LogDevice#write',
],
hideUnlabeled: true,
};
expect(prunedMapData.appmap.data.pruneFilter).toEqual(expectedFilter);
delete statsMapData.pruneFilter;
});
it('indicates when a map has been automatically pruned', async () => {
const filter = {
hideName: [
'function:logger/Logger::LogDevice#write',
'function:activerecord/ActiveRecord::Relation#records',
'function:actionpack/ActionDispatch::Request::Session#[]',
],
};
const serializedFilter = base64UrlEncode(JSON.stringify(filter));
const argv = {
file: statsMapFile,
filter: serializedFilter,
outputDir: projectDir,
auto: true,
};
await cmd.handler(argv);
const outPath = path.join(projectDir, 'Microposts_interface_micropost_interface.appmap.json');
const prunedMapData = await fromFile(outPath);
expect(prunedMapData.pruneFilter.auto).toBe(true);
});
it('does not write a file when --output-data is used', async () => {
const mapName = 'revoke_api_key.appmap.json';
const appMapFile = path.join(projectDir, mapName);
const outputDir = path.join(projectDir, 'out');
fs.mkdirSync(outputDir);
const argv = {
file: appMapFile,
size: '5kb',
outputData: true,
outputDir,
};
await cmd.handler(argv);
expect(fs.existsSync(path.join(outputDir, mapName))).toBe(false);
});
});