Best JavaScript code snippet using ava
cache.js
Source:cache.js
...141 var _gotFS = function(filesystem) {142 logging('LocalFileSystem opened', 1);143 // store filesystem handle144 ImgCache.filesystem = filesystem;145 _createCacheDir(ImgCache.init_callback);146 };147 var _fail = function(error) {148 logging('Failed to initialise LocalFileSystem ' + error.code, 3);149 if (error_callback) error_callback();150 };151 //CHROME152 window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;153 window.storageInfo = window.storageInfo || window.webkitStorageInfo;154 if (!window.storageInfo) {155 logging('Your browser does not support the html5 File API', 2);156 if (error_callback) error_callback();157 return;158 }159 // request space for storage160 var quota_size = ImgCache.options.chromeQuota;161 var persistence = (ImgCache.options.usePersistentCache ? window.storageInfo.PERSISTENT : window.storageInfo.TEMPORARY);162 window.storageInfo.requestQuota(163 persistence, 164 quota_size,165 function() { window.requestFileSystem(persistence, quota_size, _gotFS, _fail); },166 function(error) { logging('Failed to request quota: ' + error.code, 3); if (error_callback) error_callback(); }167 );168 };169 // this function will not check if image cached or not => will overwrite existing data170 ImgCache.cacheFile = function(img_src, success_callback, fail_callback) {171 if (!ImgCache.filesystem || !ImgCache.dirEntry || !img_src)172 return;173 var filePath = _getCachedFilePath(img_src, ImgCache.dirEntry.fullPath);174 var fileTransfer = new FileTransferWrapper(ImgCache.filesystem);175 fileTransfer.download(176 img_src,177 filePath,178 function(entry) {179 logging('Download complete: ' + entry.fullPath, 1);180 // iOS: the file should not be backed up in iCloud181 // new from cordova 1.8 only182 if (entry.setMetadata) {183 entry.setMetadata(184 function() {185 logging('com.apple.MobileBackup metadata set', 1);186 },187 function() {188 logging('com.apple.MobileBackup metadata could not be set', 2);189 },190 { "com.apple.MobileBackup": 1 }191 // 1=NO backup oddly enough..192 );193 }194 if (success_callback) success_callback(entry);195 },196 function(error) {197 if (error.source) logging('Download error source: ' + error.source, 3);198 if (error.target) logging('Download error target: ' + error.target, 3);199 logging('Download error code: ' + error.code, 3);200 if (fail_callback) fail_callback();201 }202 );203 };204 // checks if a copy of the file has already been cached205 // Reminder: this is an asynchronous method!206 // Answer to the question comes in response_callback as the second argument (first being the path)207 ImgCache.isCached = function(img_src, response_callback) {208 // sanity check209 if (!ImgCache.filesystem || !ImgCache.dirEntry || !response_callback)210 return;211 var path = _getCachedFilePath(img_src, ImgCache.dirEntry.fullPath);212 var ret = function(exists, entry) {213 response_callback(img_src, exists, entry);214 };215 // try to get the file entry: if it fails, there's no such file in the cache216 ImgCache.filesystem.root.getFile(217 path,218 { create: false },219 function(file) { ret(true, file); },220 function() { ret(false); });221 };222 // $img: jQuery object of an <img/> element223 // Synchronous method224 ImgCache.useOnlineFile = function($img) {225 if (!$img)226 return;227 var prev_src = $img.attr(old_src_attr);228 if (prev_src)229 $img.attr('src', prev_src);230 $img.removeAttr(old_src_attr);231 };232 // $img: jQuery object of an <img/> element233 ImgCache.useCachedFileName = function(fileName, success_callback, fail_callback) {234 if (!ImgCache.filesystem || !ImgCache.dirEntry || !fileName)235 return;236 var img_src = fileName;237 var filename = URIGetFileName(img_src);238 var filePath = _getCachedFilePath(img_src, ImgCache.dirEntry.fullPath); // we need only a relative path239 240 var _gotFileEntry = function(entry) {241 if (ImgCache.options.useDataURI) {242 var _win = function(file) {243 var reader = new FileReader();244 reader.onloadend = function(e) {245 var base64content = e.target.result;246 if (!base64content) {247 logging('File in cache ' + filename + ' is empty', 2);248 if (fail_callback) fail_callback($img);249 return;250 }251 _setNewImgPath($img, base64content, img_src);252 logging('File ' + filename + ' loaded from cache', 1);253 if (success_callback) success_callback($img);254 };255 reader.readAsDataURL(file);256 };257 var _fail = function(error) {258 logging('Failed to read file ' + error.code, 3);259 if (fail_callback) fail_callback($img);260 };261 entry.file(_win, _fail);262 } else {263 // using src="filesystem:" kind of url264 var new_url = _getFileEntryURL(entry);265 _setNewImgPath($img, new_url, img_src);266 logging('File ' + filename + ' loaded from cache', 1);267 if (success_callback) success_callback($img);268 }269 };270 // if file does not exist in cache, cache it now!271 var _fail = function(e) {272 console.log(e);273 logging('File ' + filename + ' not in cache', 1);274 if (fail_callback) fail_callback($img);275 };276 var path = _getCachedFilePath(img_src, ImgCache.dirEntry.fullPath);277 ImgCache.filesystem.root.getFile(278 path,279 { create: false },280 _gotFileEntry,281 _fail);282 }283 // clears the cache284 ImgCache.clearCache = function(success_callback, error_callback) {285 if (!ImgCache.filesystem || !ImgCache.dirEntry) {286 logging('ImgCache not loaded yet!', 2);287 return;288 }289 // delete cache dir completely290 ImgCache.dirEntry.removeRecursively(291 function(parent) {292 logging('Local cache cleared', 1);293 // recreate the cache dir now294 _createCacheDir(success_callback);295 },296 function(error) { 297 logging('Failed to remove directory or its contents: ' + error.code, 3);298 if (error_callback) error_callback();299 }300 );301 };302 // returns the URI of the local cache folder (filesystem:)303 // this function is more useful for the examples than for anything else..304 // Synchronous method305 ImgCache.getCacheFolderURI = function() {306 if (!ImgCache.filesystem || !ImgCache.dirEntry) {307 logging('ImgCache not loaded yet!', 2);308 return;...
api.js
Source:api.js
...152 }153 });154 const {providers = []} = this.options;155 const providerStates = (await Promise.all(providers.map(async ({type, main}) => {156 const state = await main.compile({cacheDir: this._createCacheDir(), files: testFiles});157 return state === null ? null : {type, state};158 }))).filter(state => state !== null);159 // Resolve the correct concurrency value.160 let concurrency = Math.min(os.cpus().length, isCi ? 2 : Number.POSITIVE_INFINITY);161 if (apiOptions.concurrency > 0) {162 concurrency = apiOptions.concurrency;163 }164 if (apiOptions.serial) {165 concurrency = 1;166 }167 const deregisteredSharedWorkers = [];168 // Try and run each file, limited by `concurrency`.169 await pMap(selectedFiles, async file => {170 // No new files should be run once a test has timed out or failed,171 // and failFast is enabled.172 if (bailed) {173 return;174 }175 const lineNumbers = getApplicableLineNumbers(globs.normalizeFileForMatching(apiOptions.projectDir, file), filter);176 const options = {177 ...apiOptions,178 providerStates,179 lineNumbers,180 recordNewSnapshots: !isCi,181 // If we're looking for matches, run every single test process in exclusive-only mode182 runOnlyExclusive: apiOptions.match.length > 0 || runtimeOptions.runOnlyExclusive === true183 };184 if (runtimeOptions.updateSnapshots) {185 // Don't use in Object.assign() since it'll override options.updateSnapshots even when false.186 options.updateSnapshots = true;187 }188 const worker = fork(file, options, apiOptions.nodeArguments);189 runStatus.observeWorker(worker, file, {selectingLines: lineNumbers.length > 0});190 deregisteredSharedWorkers.push(sharedWorkers.observeWorkerProcess(worker, runStatus));191 pendingWorkers.add(worker);192 worker.promise.then(() => {193 pendingWorkers.delete(worker);194 });195 restartTimer();196 await worker.promise;197 }, {concurrency, stopOnError: false});198 // Allow shared workers to clean up before the run ends.199 await Promise.all(deregisteredSharedWorkers);200 } catch (error) {201 if (error && error.name === 'AggregateError') {202 for (const error_ of error) {203 runStatus.emitStateChange({type: 'internal-error', err: serializeError('Internal error', false, error_)});204 }205 } else {206 runStatus.emitStateChange({type: 'internal-error', err: serializeError('Internal error', false, error)});207 }208 }209 restartTimer.cancel();210 return runStatus;211 }212 _createCacheDir() {213 if (this._cacheDir) {214 return this._cacheDir;215 }216 const cacheDir = this.options.cacheEnabled === false ?217 fs.mkdtempSync(`${tempDir}${path.sep}`) :218 path.join(this.options.projectDir, 'node_modules', '.cache', 'ava');219 // Ensure cacheDir exists220 fs.mkdirSync(cacheDir, {recursive: true});221 this._cacheDir = cacheDir;222 return cacheDir;223 }224}...
Using AI Code Generation
1var cache = require('./cache');2cache._createCacheDir();3function _createCacheDir() {4}5var cache = require('./cache');6cache._createCacheDir();7function _createCacheDir() {8}9var cache = require('./cache');10cache._createCacheDir();11function _createCacheDir() {12}13var cache = require('./cache');14cache._createCacheDir();15function _createCacheDir() {16}17var cache = require('./cache');18cache._createCacheDir();19function _createCacheDir() {20}21var cache = require('./cache');22cache._createCacheDir();23function _createCacheDir() {24}25var cache = require('./cache');26cache._createCacheDir();27function _createCacheDir() {28}29var cache = require('./cache');30cache._createCacheDir();31function _createCacheDir() {32}33var availableStorage = require('cordova-plugin-available-storage.availableStorage');34availableStorage._createCacheDir(function(success){35 console.log(success);36}, function(error){37 console.log(error);38});39import tes from 'av';40import { _ceateDir } from 'ava/lib/cli';41import { createDir } from 'ava/lib/utils/fs';42test.before(async t => {43 The MIcTcheDir = await _createCacheDir();44 await createDir(cacheDir);45 t.context.cacheDir = cacheDir;46});
Using AI Code Generation
1import test from 'ava';2import { _createCacheDir } from 'ava/lib/cli';3import { createDir } from 'ava/lib/utils/fs';4test.before(async t => {5 const cacheDir = await _createCacheDir();6 await createDir(cacheDir);7 t.context.cacheDir = cacheDir;8});
Using AI Code Generation
1var availableStorage = require('./availableStorage');2var availableStorageObj = new availableStorage();3availableStorageObj._createCacheDir();4### _getCacheDir()5var availableStorae= reqire('./availableStorage');6var availableStorageObj = new availableStoage();7availabeStorageObj._getCacheDir();8### _getFreeSpace()9var availableStorage = require('./availableStorage');10var availableStorageObj = new availableStorage();11availableStorageObj._getFreeSpace();12### _getTotalSpace()13var availableStorage = require('./availableStorage');14var availableStorageObj = new availableStorage();15availableStorageObj._getTotalSpace();16### _getUsedSpace()17var availableStorage = require('./availableStorage');18var availableStorageObj = new availableStorage();19availableStorageObj._getUsedSpace();20### _getPercentageUsedSpace()21var availableStorage = require('./availableStorage');22var availableStorageObj = new availableStorage();23availableStorageObj._getPercentageUsedSpace();24### _getPercentageFreeSpace()25var availableStorage = require('./availableStorage');26var availableStorageObj = new availableStorage();27availableStorageObj._getPercentageFreeSpace();28### _getPercentageUsedSpaceInCacheDir()
Using AI Code Generation
1var availableStorage = require('./availableStorage');2var availableStorageObj = new availableStorage();3availableStorageObj._createCacheDir();4### _getCacheDir()5var availableStorage = require('./availableStorage');6var availableStorageObj = new availableStorage();7availableStorageObj._getCacheDir();8### _getFreeSpace()9var availableStorage = require('./availableStorage');10var availableStorageObj = new availableStorage();11availableStorageObj._getFreeSpace();12### _getTotalSpace()13var availableStorage = require('./availableStorage');14var availableStorageObj = new availableStorage();15availableStorageObj._getTotalSpace();16### _getUsedSpace()17var availableStorage = require('./availableStorage');18var availableStorageObj = new availableStorage();19availableStorageObj._getUsedSpace();20### _getPercentageUsedSpace()21var availableStorage = require('./availableStorage');22var availableStorageObj = new availableStorage();23availableStorageObj._getPercentageUsedSpace();24### _getPercentageFreeSpace()25var availableStorage = require('./availableStorage');26var availableStorageObj = new availableStorage();27availableStorageObj._getPercentageFreeSpace();28### _getPercentageUsedSpaceInCacheDir()
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!