From 0a64f4bd5374d5f8fb4b040cca92f698d8ab1670 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Wed, 9 Oct 2024 00:13:15 +0200 Subject: [PATCH] fix(client): ensure source is added to files (#56601) --- .../Challenges/rechallenge/transformers.js | 5 +++- shared/utils/polyvinyl.js | 11 ++++++- shared/utils/polyvinyl.test.js | 29 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 shared/utils/polyvinyl.test.js diff --git a/client/src/templates/Challenges/rechallenge/transformers.js b/client/src/templates/Challenges/rechallenge/transformers.js index e4419e6db08..14d04689613 100644 --- a/client/src/templates/Challenges/rechallenge/transformers.js +++ b/client/src/templates/Challenges/rechallenge/transformers.js @@ -14,7 +14,8 @@ import { transformContents, transformHeadTailAndContents, setExt, - compileHeadTail + compileHeadTail, + createSource } from '../../../../../shared/utils/polyvinyl'; import { WorkerExecutor } from '../utils/worker-executor'; @@ -275,6 +276,7 @@ const htmlTransformer = cond([ ]); export const getTransformers = loopProtectOptions => [ + createSource, replaceNBSP, babelTransformer(loopProtectOptions), partial(compileHeadTail, ''), @@ -282,6 +284,7 @@ export const getTransformers = loopProtectOptions => [ ]; export const getPythonTransformers = () => [ + createSource, replaceNBSP, partial(compileHeadTail, '') ]; diff --git a/shared/utils/polyvinyl.js b/shared/utils/polyvinyl.js index 754ae2ca527..25a74dc8ded 100644 --- a/shared/utils/polyvinyl.js +++ b/shared/utils/polyvinyl.js @@ -20,7 +20,7 @@ const invariant = require('invariant'); // contents: String, // history?: [...String], // }) => PolyVinyl, throws -function createPoly({ name, ext, contents, history, ...rest } = {}) { +function createPoly({ name, ext, contents, history, ...rest }) { invariant(typeof name === 'string', 'name must be a string but got %s', name); invariant(typeof ext === 'string', 'ext must be a string, but was %s', ext); @@ -147,11 +147,20 @@ function transformHeadTailAndContents(wrap, poly) { }; } +// createSource(poly: PolyVinyl) => PolyVinyl +function createSource(poly) { + return { + ...poly, + source: poly.source || poly.contents + }; +} + module.exports = { createPoly, isPoly, setContent, setExt, + createSource, compileHeadTail, regeneratePathAndHistory, transformContents, diff --git a/shared/utils/polyvinyl.test.js b/shared/utils/polyvinyl.test.js new file mode 100644 index 00000000000..8263e033146 --- /dev/null +++ b/shared/utils/polyvinyl.test.js @@ -0,0 +1,29 @@ +import polyvinyl from './polyvinyl'; + +const polyData = { + name: 'test', + ext: 'js', + contents: 'var hello = world;', + history: ['test.js'] +}; + +describe('createSource', () => { + it('should return a vinyl object with a source matching the contents', () => { + const original = polyvinyl.createPoly(polyData); + + const updated = polyvinyl.createSource(original); + expect(original).not.toHaveProperty('source'); + expect(updated).toHaveProperty('source', 'var hello = world;'); + expect(updated).toMatchObject(original); + }); + + it('should not update the source if it already exists', () => { + const original = polyvinyl.createPoly({ + ...polyData, + source: 'const hello = world;' + }); + + const updated = polyvinyl.createSource(original); + expect(updated).toStrictEqual(original); + }); +});