Skip to content

Commit

Permalink
Fixes for --skip-pre & --skip-post methods (#15)
Browse files Browse the repository at this point in the history
* Fixes for --skip-pre & --skip-post methods

Co-authored-by: Tim <>
  • Loading branch information
thim81 committed Aug 18, 2021
1 parent 8925c93 commit 0b10f7d
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.6.0...HEAD)

### Fixed

- Fixes a bug that when using the --skip-pre/--skip-post options was passed, the converter did not remove the pre-request/post-request scripts at the collection or folder level. (grafana#105)

### Changed

- Updated readme to remove the docker hub reference to prevent confusion with the original package, since this fork does not build dockers.
Expand Down
32 changes: 20 additions & 12 deletions lib/common/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,32 @@ function readPost(node, block, result = block) {
const events = aid.spread(node.events);
const posts = events.filter(event => event.listen === 'test');
const sections = [];
for (const event of posts) {
if (event.disabled) {
continue;
}
const exec = event.script.exec;
if (exec.disabled) {
continue;
}
const logic = exec.join('\n').trim();
if (!logic) {
continue;

// Only skip if --skip-post is set
if (shouldIncludePost(result)) {
for (const event of posts) {
if (event.disabled) {
continue;
}
const exec = event.script.exec;
if (exec.disabled) {
continue;
}
const logic = exec.join('\n').trim();
if (!logic) {
continue;
}
sections.push(logic);
}
sections.push(logic);
}
if (sections.length) {
block.post = sections.join('\n\n');
detectFeature(block.post, result);
}
}

function shouldIncludePost(result) {
return !result.setting.skip || !result.setting.skip.post;
}

module.exports = readPost;
32 changes: 20 additions & 12 deletions lib/common/pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,32 @@ function readPre(node, block, result = block) {
const events = aid.spread(node.events);
const pres = events.filter(event => event.listen === 'prerequest');
const sections = [];
for (const event of pres) {
if (event.disabled) {
continue;
}
const exec = event.script.exec;
if (exec.disabled) {
continue;
}
const logic = exec.join('\n').trim();
if (!logic) {
continue;

// Only skip if --skip-pre is set
if (shouldIncludePre(result)) {
for (const event of pres) {
if (event.disabled) {
continue;
}
const exec = event.script.exec;
if (exec.disabled) {
continue;
}
const logic = exec.join('\n').trim();
if (!logic) {
continue;
}
sections.push(logic);
}
sections.push(logic);
}
if (sections.length) {
block.pre = sections.join('\n\n');
detectFeature(block.pre, result);
}
}

function shouldIncludePre(result) {
return !result.setting.skip || !result.setting.skip.pre;
}

module.exports = readPre;
277 changes: 277 additions & 0 deletions test/int/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,143 @@ export default function() {
);
});

test('pre request --skip-pre', async t => {
const options = {
skip: {
pre: true,
},
};
const [main] = await convertFile('test/material/2/pre-request.json', options);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
}
`
);
});

test('pre collection --skip-pre', async t => {
const options = {
skip: {
pre: true,
},
};
const [main] = await convertFile(
'test/material/2/pre-collection.json',
options
);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
}
`
);
});

test('pre folder --skip-pre', async t => {
const options = {
skip: {
pre: true,
},
};
const [main] = await convertFile('test/material/2/pre-folder.json', options);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
import { group } from "k6";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
group("TestFolder", function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
});
}
`
);
});

test('pre nested --skip-pre', async t => {
const options = {
skip: {
pre: true,
},
};
const [main] = await convertFile('test/material/2/pre-nested.json', options);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
import { group } from "k6";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
group("TestFolder", function() {
group("TestFolder2", function() {
group("TestFolder3", function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
});
});
});
}
`
);
});

test('post request', async t => {
const [main] = await convertFile('test/material/2/post-request.json');
t.is(
Expand Down Expand Up @@ -312,3 +449,143 @@ export default function() {
`
);
});

test('post request --skip-pre', async t => {
const options = {
skip: {
post: true,
},
};
const [main] = await convertFile(
'test/material/2/post-request.json',
options
);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
}
`
);
});

test('post collection --skip-pre', async t => {
const options = {
skip: {
post: true,
},
};
const [main] = await convertFile(
'test/material/2/post-collection.json',
options
);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
}
`
);
});

test('post folder --skip-pre', async t => {
const options = {
skip: {
post: true,
},
};
const [main] = await convertFile('test/material/2/post-folder.json', options);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
import { group } from "k6";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
group("TestFolder", function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
});
}
`
);
});

test('post nested --skip-pre', async t => {
const options = {
skip: {
post: true,
},
};
const [main] = await convertFile('test/material/2/post-nested.json', options);
t.is(
main,
`// Auto-generated by the postman-to-k6 converter
import "./libs/shim/core.js";
import { group } from "k6";
export let options = { maxRedirects: 4 };
const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
options
});
export default function() {
group("TestFolder", function() {
group("TestFolder2", function() {
group("TestFolder3", function() {
postman[Request]({
name: "TestRequest",
method: "GET",
address: "http://example.com/"
});
});
});
});
}
`
);
});

0 comments on commit 0b10f7d

Please sign in to comment.