From e6d13142541bb291b81993a6d38ae224316e6f75 Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Thu, 25 Jul 2019 00:47:46 -0700 Subject: [PATCH] Use the alloc file factory for the fs stat and fs ls end points --- ui/mirage/config.js | 82 ++++++++++--------------------- ui/mirage/factories/alloc-file.js | 19 ++++--- 2 files changed, 39 insertions(+), 62 deletions(-) diff --git a/ui/mirage/config.js b/ui/mirage/config.js index 777124d7b..daa5adfda 100644 --- a/ui/mirage/config.js +++ b/ui/mirage/config.js @@ -305,63 +305,35 @@ export default function() { return logEncode(logFrames, logFrames.length - 1); }; - const clientAllocationFSLsHandler = function(schema, { queryParams }) { - if (queryParams.path.endsWith('empty-directory')) { - return []; - } else if (queryParams.path.endsWith('directory')) { - return [ - { - Name: 'another', - IsDir: true, - ModTime: moment().format(), - }, - ]; - } else if (queryParams.path.endsWith('another')) { - return [ - { - Name: 'something.txt', - IsDir: false, - ModTime: moment().format(), - }, - ]; - } else { - return [ - { - Name: '🤩.txt', - IsDir: false, - Size: 1919, - ModTime: moment() - .subtract(2, 'day') - .format(), - }, - { - Name: '🙌🏿.txt', - IsDir: false, - ModTime: moment() - .subtract(2, 'minute') - .format(), - }, - { - Name: 'directory', - IsDir: true, - Size: 3682561, - ModTime: moment() - .subtract(1, 'year') - .format(), - }, - { - Name: 'empty-directory', - IsDir: true, - ModTime: moment().format(), - }, - ]; - } + const clientAllocationFSLsHandler = function({ allocFiles }, { queryParams }) { + // Ignore the task name at the beginning of the path + const filterPath = queryParams.path.substr(queryParams.path.indexOf('/') + 1); + + const files = allocFiles.where( + file => + (!filterPath || file.path.startsWith(filterPath)) && + file.path.length > filterPath.length && + !file.path.substr(filterPath.length + 1).includes('/') + ); + + return this.serialize(files); }; - const clientAllocationFSStatHandler = function(schema, { queryParams }) { - return { - IsDir: !queryParams.path.endsWith('.txt'), - }; + const clientAllocationFSStatHandler = function({ allocFiles }, { queryParams }) { + // Ignore the task name at the beginning of the path + const filterPath = queryParams.path.substr(queryParams.path.indexOf('/') + 1); + + // Root path + if (!filterPath) { + return this.serialize({ + IsDir: true, + ModTime: new Date(), + }); + } + + // Either a file or a nested directory + const file = allocFiles.where({ path: filterPath }).models[0]; + return this.serialize(file); }; // Client requests are available on the server and the client diff --git a/ui/mirage/factories/alloc-file.js b/ui/mirage/factories/alloc-file.js index 3329b6498..b6232dced 100644 --- a/ui/mirage/factories/alloc-file.js +++ b/ui/mirage/factories/alloc-file.js @@ -1,6 +1,7 @@ import { Factory, faker, trait } from 'ember-cli-mirage'; import { pickOne } from '../utils'; +const REF_TIME = new Date(); const EMOJI = '🏆 💃 🤩 🙌🏿 🖨'.split(' '); const makeWord = () => Math.round(Math.random() * 10000000 + 50000).toString(36); const makeSentence = (count = 10) => @@ -67,16 +68,18 @@ export default Factory.extend({ return fileTypeMapping[this.fileType] || null; }, - name() { - const fileName = `${faker.hacker.noun().dasherize()}-${pickOne(EMOJI)}${ - this.isDir ? '' : `.${this.fileType}` - }`; - + path() { if (this.parent) { - return `${this.parent.name}/${fileName}`; + return `${this.parent.name}/${this.name}`; } - return fileName; + return this.name; + }, + + name() { + return `${faker.hacker.noun().dasherize()}-${pickOne(EMOJI)}${ + this.isDir ? '' : `.${this.fileType}` + }`; }, body() { @@ -88,6 +91,8 @@ export default Factory.extend({ return this.body.length; }, + modTime: () => faker.date.past(2 / 365, REF_TIME), + dir: trait({ isDir: true, afterCreate(allocFile, server) {