How to use fs.promises.stat method in ava

Best JavaScript code snippet using ava

get-tld.tests.js

Source:get-tld.tests.js Github

copy

Full Screen

1const proxyquire = require('proxyquire');2const sinon = require('sinon');3const chai = require('chai');4const { EventEmitter } = require('events');5const { PassThrough: PassThroughStream } = require('stream');6chai.use(require('sinon-chai'));7const expect = chai.expect;8describe('getTLD', function () {9 this.timeout(5000);10 beforeEach(setup);11 let PublicSuffixData;12 let fs, request;13 it('defaults the cache file location to USERPROFILE if that variable exists', async function () {14 process.env.USERPROFILE = 'userProfilePath';15 const data = new PublicSuffixData();16 await data.getTLD('somedomain.com');17 expect(fs.promises.stat).to.have.been.calledWithMatch(sinon.match(function (path) {18 return path.indexOf('userProfilePath') >= 0;19 }));20 });21 it('defaults the cache file location to HOME if that variable exists', function () {22 delete process.env.USERPROFILE;23 process.env.HOME = 'userHomeDir';24 const data = new PublicSuffixData();25 return data.getTLD('somedomain.com')26 .then(function () {27 expect(fs.promises.stat).to.have.been.calledWithMatch(sinon.match(function (path) {28 return path.indexOf('userHomeDir') >= 0;29 }));30 });31 });32 it('retrieves the list from the Internet if the cached file does not exist', function () {33 fs.promises.stat.withArgs(sinon.match(/\.publicsuffix\.org$/)).rejects({ code: 'ENOENT' });34 const data = new PublicSuffixData();35 return data.getTLD('somedomain.com')36 .then(function () {37 expect(request).to.have.been.calledWith('https://publicsuffix.org/list/effective_tld_names.dat');38 });39 });40 it('yields an error if there is no cached data and data retrieval fails', async function () {41 fs.promises.stat.withArgs(sinon.match(/\.publicsuffix\.org$/)).rejects({ code: 'ENOENT' });42 request.returns(createMockErrorStream());43 const data = new PublicSuffixData();44 let caughtError;45 try {46 await data.getTLD('somedomain.com');47 } catch (err) {48 caughtError = err;49 }50 51 expect(caughtError).to.exist;52 });53 it('retrieves the list from the Internet if the cached file exceeds the time-to-stale setting', function () {54 fs.promises.stat.withArgs(sinon.match(/\.publicsuffix\.org$/)).resolves({ mtime: new Date(Date.now() - 1296000000) });55 const data = new PublicSuffixData();56 return data.getTLD('somedomain.com')57 .then(function () {58 expect(request).to.have.been.calledWith('https://publicsuffix.org/list/effective_tld_names.dat');59 });60 });61 it('does not yield an error if there is stale but not expired cached data in memory and data retrieval fails', async function () {62 const data = new PublicSuffixData({ tts: 0.005, ttl: 1 });63 await data.getTLD('somedomain.com');64 fs.promises.stat.rejects({ code: 'ENOENT' });65 fs.promises.readFile.rejects({ code: 'ENOENT' });66 await delay(10);67 fs.promises.stat.rejects({ code: 'ENOENT' });68 request.returns(createMockErrorStream());69 await data.getTLD('somedomain.com');70 });71 it('does not yield an error if there is stale but not expired cached data on disk and data retrieval fails', async function () {72 request.returns(createMockErrorStream());73 fs.promises.stat.withArgs(sinon.match(/\.publicsuffix\.org$/)).resolves({ mtime: new Date(Date.now() - 150000) });74 const data = new PublicSuffixData({ tts: 100, ttl: 200 });75 await data.getTLD('somedomain.com');76 });77 it('yields an error if there is expired cached data and data retrieval fails', async function () {78 fs.promises.stat.withArgs(sinon.match(/\.publicsuffix\.org$/)).resolves({ mtime: new Date(Date.now() - 300000) });79 request.returns(createMockErrorStream());80 const data = new PublicSuffixData({ tts: 100, ttl: 200 });81 let caughtError = null;82 try {83 await data.getTLD('somedomain.com');84 } catch (err) {85 caughtError = err;86 }87 88 expect(caughtError).to.exist;89 });90 it('does not retrieve the list from the Internet if there is a non-stale cached file', function () {91 fs.promises.stat.withArgs(sinon.match(/\.publicsuffix\.org$/)).resolves({ mtime: new Date() });92 const data = new PublicSuffixData();93 return data.getTLD('somedomain.com')94 .then(function () {95 expect(request).to.have.not.been.called;96 });97 });98 it('retrieves the list from the Internet if there is a non-stale cached file that gets a read error', function () {99 fs.promises.stat.withArgs(sinon.match(/\.publicsuffix\.org$/)).resolves({ mtime: new Date() });100 fs.promises.readFile.rejects(new Error());101 const data = new PublicSuffixData();102 return data.getTLD('somedomain.com')103 .then(function () {104 expect(request).to.have.been.called;105 });106 });107 it('saves the retrieved list to the cache location', async function () {108 const cacheFile = sinon.match(/\.publicsuffix\.org$/);109 fs.promises.stat.withArgs(cacheFile).rejects({ code: 'ENOENT' });110 const data = new PublicSuffixData();111 await data.getTLD('somedomain.com');112 await delay(1);113 expect(fs.promises.writeFile).to.have.been.calledWithMatch(cacheFile);114 });115 it('caches the TLDs to memory so subsequent disk reads or Internet fetches are not needed', function () {116 const data = new PublicSuffixData();117 return data.getTLD('somedomain.com')118 .then(function () {119 fs.promises.stat.reset();120 return data.getTLD('somethingelse.net');121 })122 .then(function () {123 expect(fs.promises.stat).to.have.not.been.called;124 })125 });126 it('returns data from memory when valid but stale, so fetch is performed out-of-band', async function () {127 this.timeout(5000);128 fs.promises.stat.rejects({ code: 'ENOENT' });129 request.returns(createMockStream('uk'));130 const data = new PublicSuffixData({ tts: 1, ttl: 1000000 });131 let tld = await data.getTLD('somedomain.co.uk');132 expect(tld).to.equal('uk');133 await delay(2000);134 request.returns(createMockStream('uk\nco.uk'));135 tld = await data.getTLD('somedomain.co.uk');136 expect(tld).to.equal('uk'); // We still serve up stale data137 await delay(1000);138 tld = await data.getTLD('somedomain.co.uk');139 expect(tld).to.equal('co.uk');140 });141 it('handles single-segment TLDs', function () {142 const cacheFile = sinon.match(/\.publicsuffix\.org$/);143 fs.promises.stat.withArgs(cacheFile).rejects({ code: 'ENOENT' });144 request.returns(createMockStream('com'));145 const data = new PublicSuffixData();146 return data.getTLD('somedomain.com')147 .then(function (tld) {148 expect(tld).to.equal('com');149 });150 });151 it('handles two-segment TLDs', function () {152 const cacheFile = sinon.match(/\.publicsuffix\.org$/);153 fs.promises.stat.withArgs(cacheFile).rejects({ code: 'ENOENT' });154 request.returns(createMockStream('co\nuk\nco.uk'));155 const data = new PublicSuffixData();156 return data.getTLD('foo.co.uk')157 .then(function (tld) {158 expect(tld).to.equal('co.uk');159 });160 });161 it('handles exception rules', function () {162 const cacheFile = sinon.match(/\.publicsuffix\.org$/);163 fs.promises.stat.withArgs(cacheFile).rejects({ code: 'ENOENT' });164 request.returns(createMockStream('*.kawasaki.jp\n!city.kawasaki.jp'));165 const data = new PublicSuffixData();166 return data.getTLD('city.kawasaki.jp')167 .then(function (tld) {168 expect(tld).to.equal('kawasaki.jp');169 });170 });171 it('ignores comments', function () {172 const cacheFile = sinon.match(/\.publicsuffix\.org$/);173 fs.promises.stat.withArgs(cacheFile).rejects({ code: 'ENOENT' });174 request.returns(createMockStream('jp\n// jp geographic type names\n*.kawasaki.jp'));175 const data = new PublicSuffixData();176 return data.getTLD('foo.kawasaki.jp')177 .then(function (tld) {178 expect(tld).to.equal('foo.kawasaki.jp');179 });180 });181 function setup() {182 fs = {183 promises: {184 stat: sinon.stub().resolves({ mtime: new Date() }),185 readFile: sinon.stub().resolves('{}'),186 writeFile: sinon.stub().resolves()187 }188 };189 request = sinon.stub().returns(createMockStream(''));190 PublicSuffixData = proxyquire.noCallThru()('../../lib/public-suffix-data', {191 fs: fs,192 request: request193 });194 }195 function createMockStream(content) {196 const stream = new PassThroughStream();197 stream.end(content);198 return stream;199 }200 function createMockErrorStream() {201 const mockStream = new EventEmitter();202 mockStream.on = sinon.stub().withArgs('error').yields(new Error('Boom'));203 mockStream.pipe = function (writable) { return writable; };204 return mockStream;205 }206 function delay(ms) {207 return new Promise(resolve => setTimeout(resolve, ms));208 }...

Full Screen

Full Screen

dev.js

Source:dev.js Github

copy

Full Screen

...43 if (withMinExt(path)) {44 // Minizize image45 const relPath = join(publicPath, removeMinExt(path))46 const ext = extname(relPath)47 if (fs.existsSync(relPath) && (await fs.promises.stat(relPath)).isFile()) {48 const processed = await sharp(relPath).resize(100).toBuffer()49 res.type(ext.substring(1)).send(processed)50 } else {51 next()52 }53 } else {54 const relPath = join(publicPath, path)55 const ext = extname(relPath)56 if (fs.existsSync(relPath) && (await fs.promises.stat(relPath)).isFile()) {57 res.type(ext.substring(1)).send(await fs.promises.readFile(relPath))58 } else {59 next()60 }61 }62 break63 }64 case '.html':65 {66 const relPath = join(pagesPath, replaceFileExt(path, '.pug'))67 if (fs.existsSync(relPath) && (await fs.promises.stat(relPath)).isFile()) {68 const page = pug.compileFile(relPath)69 res.type('html').send(page(pugOptions))70 } else {71 next()72 }73 break74 }75 case '.css':76 {77 const relPath = join(stylesPath, replaceFileExt(path, '.styl'))78 if (fs.existsSync(relPath) && (await fs.promises.stat(relPath)).isFile()) {79 const cssData = stylus(await fs.promises.readFile(relPath, { encoding: 'utf8' }))80 .use(nib())81 .import('nib')82 .render()83 res.type('css').send(cssData)84 } else {85 next()86 }87 break88 }89 default:90 {91 const relPath = join(publicPath, path)92 const ext = extname(relPath)93 if (fs.existsSync(relPath) && (await fs.promises.stat(relPath)).isFile()) {94 res.type(ext.substring(1)).send(await fs.promises.readFile(relPath))95 } else {96 next()97 }98 }99 }100})101app.use((_req, res) => {102 const notFountPath = join(pagesPath, '404.pug')103 res.type('html').send(pug.compileFile(notFountPath)(pugOptions)).end()104})105app.listen(8080, () => {106 console.log('Start listening on port http://localhost:8080')107})

Full Screen

Full Screen

fs.state.js

Source:fs.state.js Github

copy

Full Screen

1// Node.js program to demonstrate the 2// fsPromises.stat() method 3 4// Import the filesystem module 5const fsPromises = require("fs").promises; 6(async () => { 7 try { 8 // Using the fsPromises.stat() method 9 const stats = await fsPromises.stat( 10 "GFG.txt"); 11 console.log(stats.isDirectory()); 12 } 13 catch (error) { 14 console.log(error); 15 } ...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs').promises;2fs.stat('file.txt')3 .then((stats) => {4 console.log(`isFile: ${stats.isFile()}`);5 console.log(`isDirectory: ${stats.isDirectory()}`);6 console.log(`isBlockDevice: ${stats.isBlockDevice()}`);7 console.log(`isCharacterDevice: ${stats.isCharacterDevice()}`);8 console.log(`isFIFO: ${stats.isFIFO()}`);9 console.log(`isSocket: ${stats.isSocket()}`);10 })11 .catch((err) => console.error(err));

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs');2const { promisify } = require('util');3const stat = promisify(fs.stat);4async function main() {5 const stats = await stat('test.js');6 console.log(`This file is ${stats.size} bytes`);7}8main();9const fs = require('fs');10const { promisify } = require('util');11const stat = promisify(fs.stat);12async function main() {13 const stats = await stat('test.js');14 console.log(`This file is ${stats.size} bytes`);15}16main();17const fs = require('fs');18const { promisify } = require('util');19const stat = promisify(fs.stat);20async function main() {21 const stats = await stat('test.js');22 console.log(`This file is ${stats.size} bytes`);23}24main();25const fs = require('fs');26const { promisify } = require('util');27const stat = promisify(fs.stat);28async function main() {29 const stats = await stat('test.js');30 console.log(`This file is ${stats.size} bytes`);31}32main();33const fs = require('fs');34const { promisify } = require('util');35const stat = promisify(fs.stat);36async function main() {37 const stats = await stat('test.js');38 console.log(`This file is ${stats.size} bytes`);39}40main();41const fs = require('fs');42const { promisify } = require('util');43const stat = promisify(fs.stat);44async function main() {45 const stats = await stat('test.js');46 console.log(`This file is ${stats.size} bytes`);47}48main();49const fs = require('fs');50const { promisify } = require('util');51const stat = promisify(fs.stat);

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs');2const { promisify } = require('util');3const stat = promisify(fs.stat);4stat('/etc/passwd')5 .then((stats) => {6 console.log(stats.isFile());7 })8 .catch((error) => {9 });10const fs = require('fs');11fs.stat('/etc/passwd', (err, stats) => {12 if (err) {13 }14 console.log(stats.isFile());15});16const fs = require('fs');17const { promisify } = require('util');18const lstat = promisify(fs.lstat);19lstat('/etc/passwd')20 .then((stats) => {21 console.log(stats.isFile());22 })23 .catch((error) => {24 });25const fs = require('fs');26fs.lstat('/etc/passwd', (err, stats) => {27 if (err) {28 }29 console.log(stats.isFile());30});31const fs = require('fs');32const { promisify } = require('util');33const readdir = promisify(fs.readdir);34readdir('/etc')35 .then((files) => {36 console.log(files);37 })38 .catch((error) => {39 });40const fs = require('fs');41fs.readdir('/etc', (err, files) => {42 if (err) {43 }44 console.log(files);45});

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs').promises;2async function main() {3 const stats = await fs.stat('/etc/passwd');4 console.log(`This file is owned by ${stats.uid}`);5}6main();7const fs = require('fs').promises;8async function main() {9 const stats = await fs.stat('/etc/passwd');10 console.log(`This file is owned by ${stats.uid}`);11}12main();13const fs = require('fs').promises;14async function main() {15 const stats = await fs.stat('/etc/passwd');16 console.log(`This file is owned by ${stats.uid}`);17}18main();19const fs = require('fs').promises;20async function main() {21 const stats = await fs.stat('/etc/passwd');22 console.log(`This file is owned by ${stats.uid}`);23}24main();25const fs = require('fs').promises;26async function main() {27 const stats = await fs.stat('/etc/passwd');28 console.log(`This file is owned by ${stats.uid}`);29}30main();

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs').promises;2const path = require('path');3const filePath = path.join(__dirname, 'test.txt');4fs.stat(filePath)5 .then(stats => console.log(stats))6 .catch(err => console.log(err));7{ dev: 16777220,8 birthtime: 2019-05-23T08:13:36.000Z }9The fs.Stats object is an object that describes a file. The object is returned by the fs.stat() and fs.lstat() methods. The following methods are available on the fs.Stats object:10fs.Stats.isFile()11fs.Stats.isDirectory()12fs.Stats.isBlockDevice()13fs.Stats.isCharacterDevice()14fs.Stats.isSymbolicLink() (only valid with fs.lstat())15fs.Stats.isFIFO()16fs.Stats.isSocket()

Full Screen

Using AI Code Generation

copy

Full Screen

1const fs = require('fs');2fs.promises.stat('test.js')3 .then((stats) => {4 });5 const fs = require('fs');6 fs.stat('test.js', (err, stats) => {7 if (err) throw err;8 });9 const fs = require('fs');10 try {11 const stats = fs.statSync('test.js');12 } catch (err) {13 throw err;14 }15 const fs = require('fs');16 fs.promises.stat('test.js')17 .then((stats) => {18 });19 const fs = require('fs');20 fs.stat('test.js', (err, stats) => {21 if (err) throw err;22 });23 const fs = require('fs');24 try {25 const stats = fs.statSync('test.js');26 } catch (err) {27 throw err;28 }29 const fs = require('fs');30 fs.promises.stat('test.js')31 .then((stats) => {

Full Screen

Using AI Code Generation

copy

Full Screen

1fs.promises.stat('/dev/null').then((stats) => {2 console.log(stats);3});4fs.promises.stat('/dev/null').then((stats) => {5 console.log(stats);6});7fs.stat('/dev/null', (err, stats) => {8 if (err) {9 console.log(err);10 } else {11 console.log(stats);12 }13});14fs.stat('/dev/null', (err, stats) => {15 if (err) {16 console.log(err);17 } else {18 console.log(stats);19 }20});21fs.stat('/dev/null', (err, stats) => {22 if (err) {23 console.log(err);24 } else {25 console.log(stats);26 }27});28fs.stat('/dev/null', (err, stats) => {29 if (err) {30 console.log(err);31 } else {32 console.log(stats);33 }34});35fs.stat('/dev/null', (err, stats) => {36 if (err) {37 console.log(err);38 } else {39 console.log(stats);40 }41});42fs.stat('/dev/null', (err, stats) => {43 if (err) {44 console.log(err);45 } else {46 console.log(stats);

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ava automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful