Create & write a file which is having 'execute' permission? - dart

I'm writing a bash script with dart.
Below code create a file. but that file doesn't have 'execute' permission.
so I'm not able to execute by doing ./ex.sh.
new File('ex.sh').writeAsStringSync(script_str);
Perhaps, I need to set FileStat to file. but i'm not able to find any APIs.

Haven't tried it but what if you try:
new File('ex.sh').writeAsString(script_str).then((final File file) {
return file.stat().then((final FileStat stat) => stat.mode = 777);
});

It seems this function is not yet implemented.
See code.google.com/p/dart/issues/detail?id=15078
As workaround, just made a utility function to run chmod command.
void _runBashCommandSync(GrinderContext context, String command, {String cwd, bool log: true}) {
context.log(command);
ProcessResult result =
Process.runSync('/bin/bash', ['-c', command], workingDirectory: cwd);
if (!log) return;
if (result.stdout.isNotEmpty) {
context.log(result.stdout);
}
if (result.stderr.isNotEmpty) {
context.log(result.stderr);
}
if (result.exitCode > 0) {
context.fail("exit code ${result.exitCode}");
}
}
_runBashCommandSync(context, 'chmod 777 ex.sh');

Related

What to check to catch error in a Process.run?

I’m trying to run keytool command with Process.run and if anything goes wrong I want to stop the program. I was checking stderr property of ProcessResult but it also writes it there if it is successfull. So what should I be checking to catch errors and stop the program?
await Process.run(
'keytool',
[
'-genkey',
'-v',
'-keystore',
'/Users/figengungor/key.jks',
'-keyalg',
'RSA',
'-keysize',
'2048',
'-validity',
'10000',
'-alias',
'key',
'-dname',
'cn=Unknown, ou=Unknown, o=Unknown, c=Unknown',
'-storepass',
'123456',
],)
.then((ProcessResult results) {
print('${results.stdout}');
if (results.stderr != null && results.stderr.toString().isNotEmpty) {
print('${results.stderr}');
print('EXIT CODE ${results.exitCode}');
exit(0);
}
print('Keystore file is generated at /Users/figengungor/key.jks');
});
I just checked exitCode. If it is not zero, then smt is wrong. But error message can be inside stdout or stderr. So I went like this:
if(results.exitCode!=0) {
print('STDOUT ${results.stdout}');
print('STDERR ${results.stderr}');
print('EXIT CODE ${results.exitCode}');
exit(1);
}

Call yeoman generator from code with options

I created a yeoman generator with user interaction, that can be called in the terminal like (after running npm link):
yo mygenerator --name test --path /test/path --project testproject
Now I want to include this generator in my vscode extension.
How can I call the yo generator from my typescript code when the generator when the generator is added as a package.json dependency?
So something like (pseudo code)
import { yo } from 'yeoman';
import mygenerator; // added as a dependency via package.json
const options = {
name: 'test',
path: '/test/path',
project: 'testproject',
};
yo.exec(mygenerator, options, () => {
console.log('yeoman finished')
});
Is something like this possible?
Here is a solution for that:
const env = yeoman.createEnv();
const generatorPath = '../node_modules/generator-name/generators/app/index.js';
env.getByPath(generatorDir);
env.on('error', (err: any) => {
// handle error
});
const options = {
env,
'option1': option1,
'option2': option2,
};
try {
await env.run('name', options);
} catch (err) {
// handle error
}

How to capture responsebody from newman

I want to capture the responsebody in Newman.
const newman = require('newman');
newman.run({
collection: require('./xxx.json'),
iterationData: './data.jsp',
reporters: 'cli'
}, function (err, summary) {
if (err) { throw err; }
console.log('collection run complete!');
console.log(summary);
});
I use the code above. it works fine but I want to capture the json output here from the call. How can I achieve it?
Perhaps you used a wrong term for retrieving json response body. If you want to just get the response body you need to parse JSON returned and to save it to a variable.
If you would use newman to run through command line there are everything is super simple:
let body = JSON.parse(responseBody)
console.log(body)
and after test where you need to see the response you put this 2 lines of code.
But with your case perhaps you need that:
1) Callback option
const newman = require('newman');
newman.run({
collection: require('./xxx.json'),
iterationData: './data.jsp',
reporters: 'cli'
}, function (err, summary) {
if (err) { throw err; }
console.log('collection run complete!');
console.log(summary);
})
.on('request', function (err, data) {
// err, data can be used to write to files using the node fs module.
});
or the better and modern option:
let response = await newman.run({
collection: 'collection',
environment: 'env',
})
.on('request', async function (err, data) {
// err, data can be used to write to files using the node fs module.
});
console.log(response)
Not sure I will be working as expected, but at least try.
Btw, where do you run these tests? just in clear env or use some runner framework.
Postman return execution summary in Callback function. after execution if you save the summary in callback and return it. you can access request/response/ headers.
function runcollection(callback){
newman.run({
collection: 'C:\\newman\\PMP Dependency latest collection\\Updated\\TestCollection.postman_collection.json',
environment: 'C:\\newman\\PMP Dependency latest collection\\Updated\\Test.postman_environment.json',
iterationCount :1
},function(error, summary){
callback(summary)
});
}
runcollection(result => {console.log(result.run.executions[0].response.stream.toString())});

What is the mechanism to incorporate send and download postman option from within Newman call

What is the mechanism to incorporate send and download postman option from within Newman call?
Output of my REST call is file (image/binary). On running with Newman I don't see the output. Is there any way to save the contents in a file.
as of now newman do not have this feature. but you can have a workaround where you can read the output stream and write it into the file at desired location .
attaching sample code :
var i = 0,
fs = require('fs'),
newman = require('newman'); // ensure that you have run "npm i newman" in the same directory as this file
newman.run({
// run options go here
}, function (err, summary) {
// handle collection run err, process the run summary here
}).on('request', function (err, execution) { // This is triggered when a response has been recieved
if (err) { return console.error(err); }
fs.writeFile(`response${i++}.txt`, execution.response.stream, function (error) {
if (error) { console.error(error); }
});
});

How do I get yeoman to auto overwrite files?

I have a custom generator and am writing some tests for it. Before the app.run() call I already have a app.options['skip-install'] = true to prevent npm from running. But I need it to auto-overwrite files too.
Part way through the install I get a [?] Overwrite client/file.js? (Ynaxdh) and need it to auto-answer it for me.
I have tried app.options.force = true but that doesn't seem to do anything.
I'm running in app install with this:
function installApp(obj, opts, done) {
helpers.mockPrompt(obj.app, opts);
obj.app.options['skip-install'] = true;
obj.app.run({}, function () {
async.series([
function (cb) {
if (opts._extras.addPage) {
installAppPage(obj, cb);
} else {
cb();
}
}
], done);
});
}
Then I want to run a sub-generator with this:
function installAppPage(obj, done) {
helpers.mockPrompt(obj.page, {
pageName: 'webPage',
pageType: 'web'
});
obj.page.args = ['--force']; // This isn't working
obj.page.options.force; // This isn't working either
obj.page.run([], function () {
helpers.mockPrompt(obj.page, {
pageName: 'mobilePage',
pageType: 'mobile'
});
obj.page.run({}, function () {
done();
});
});
}
The sub-generator for the page modifies a file. I need to to just overwrite it so I can test it. How do I force it? I can't be prompted while running the tests, it needs to be automated.
I think you're looking for conflicter.force
obj.page.conflicter.force = true;

Resources