Skip to content

Commit

Permalink
send source and test file contents
Browse files Browse the repository at this point in the history
  • Loading branch information
corsicanec82 committed Nov 14, 2022
1 parent 2cd7c73 commit c472d16
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 29 deletions.
58 changes: 40 additions & 18 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,56 @@ describe('run-post-actions', () => {
testData: { passed: true, output: 'some testing output', exception: null },
lintData: { passed: false, output: 'some linting output', exception: new Error('linting failed') },
};
const assignmentRelativePath = 'hexlet-course-source-ci/basics';
const filesData = {
sourceFiles: ['src/main/java/exercise/App.java'],
testFiles: ['src/test/java/exercise/AppTest.java'],
};

it('success test state', async () => {
env.STATE_checkCreatePath = checkCreatePath;
env.STATE_checkState = 'success';
env.STATE_checkData = JSON.stringify(checkData);

await expect(runPostActions(env)).resolves.not.toThrow();
const states = {
STATE_checkCreatePath: checkCreatePath,
STATE_checkState: 'success',
STATE_checkData: JSON.stringify(checkData),
STATE_assignmentPath: path.join(env.ACTION_PROJECT_PATH, assignmentRelativePath),
STATE_filesData: JSON.stringify(filesData),
};

await expect(runPostActions({ ...env, ...states })).resolves.not.toThrow();
});

it('fail test state', async () => {
env.STATE_checkCreatePath = checkCreatePath;
env.STATE_checkState = 'fail';
env.STATE_checkData = JSON.stringify(checkData);

await expect(runPostActions(env)).resolves.not.toThrow();
const states = {
STATE_checkCreatePath: checkCreatePath,
STATE_checkState: 'fail',
STATE_checkData: JSON.stringify(checkData),
STATE_assignmentPath: path.join(env.ACTION_PROJECT_PATH, assignmentRelativePath),
STATE_filesData: JSON.stringify(filesData),
};

await expect(runPostActions({ ...env, ...states })).resolves.not.toThrow();
});

it('incorrect test state', async () => {
env.STATE_checkCreatePath = checkCreatePath;
env.STATE_checkState = 'invalid';
env.STATE_checkData = JSON.stringify(checkData);

await expect(runPostActions(env)).rejects.toThrow();
const states = {
STATE_checkCreatePath: checkCreatePath,
STATE_checkState: 'invalid',
STATE_checkData: JSON.stringify(checkData),
STATE_assignmentPath: path.join(env.ACTION_PROJECT_PATH, assignmentRelativePath),
STATE_filesData: JSON.stringify(filesData),
};

await expect(runPostActions({ ...env, ...states })).rejects.toThrow();
});

it('previous stage failed before testing', async () => {
env.STATE_checkState = 'fail';

await expect(runPostActions(env)).resolves.not.toThrow();
const states = {
STATE_checkCreatePath: checkCreatePath,
STATE_checkState: 'fail',
STATE_assignmentPath: path.join(env.ACTION_PROJECT_PATH, assignmentRelativePath),
STATE_filesData: JSON.stringify(filesData),
};

await expect(runPostActions({ ...env, ...states })).resolves.not.toThrow();
});
});
2 changes: 1 addition & 1 deletion dist/run-post-actions/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/run-post-actions/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/run-tests/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/run-tests/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ const checkSchema = {
required: ['passed', 'output', 'exception'],
additionalProperties: false,
},
sourceContents: { type: 'object' },
testContents: { type: 'object' },
},
required: ['state', 'testData', 'lintData'],
required: ['state', 'testData', 'lintData', 'sourceContents', 'testContents'],
additionalProperties: false,
};

Expand Down
23 changes: 17 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import colors from 'ansi-colors';
import { HttpClient } from '@actions/http-client';

import buildRoutes from './routes.js';
import { getCourseData, getFullImageName } from './utils.js';
import {
getCourseData, getFullImageName, getFilesData, getAssignmentContents,
} from './utils.js';

const prepareCourseDirectory = async ({ verbose, coursePath, imageName }) => {
const cmdOptions = { silent: !verbose };
Expand Down Expand Up @@ -143,8 +145,11 @@ export const runTests = async (params) => {
core.saveState('checkState', 'fail');

await prepareCourseDirectory({ verbose, coursePath, imageName });
const checkData = await checkAssignment({ assignmentPath, coursePath });
const filesData = await getFilesData(coursePath, lessonSlug);
core.saveState('filesData', JSON.stringify(filesData));
core.saveState('assignmentPath', assignmentPath);

const checkData = await checkAssignment({ assignmentPath, coursePath });
core.saveState('checkData', JSON.stringify(checkData));

const { testData } = checkData;
Expand All @@ -157,20 +162,26 @@ export const runTests = async (params) => {
};

export const runPostActions = async ({ hexletToken }) => {
const checkCreatePath = core.getState('checkCreatePath');
const checkState = core.getState('checkState');
const checkDataContent = core.getState('checkData');

// NOTE: в таком случае экшн отработал с непредвиденными ошибками до запуска тестов
// дальнейшая нормальная работа экшна невозможна
if (_.isEmpty(checkDataContent)) {
core.info(colors.cyan('The testing hasn\'t started. No data to send.'));
core.info(colors.cyan('The assignment checking hasn\'t started. No data to send.'));
return;
}

const checkCreatePath = core.getState('checkCreatePath');
const checkState = core.getState('checkState');
const filesData = JSON.parse(core.getState('filesData'));
const assignmentPath = core.getState('assignmentPath');

const checkData = JSON.parse(checkDataContent);
const assignmentContents = await getAssignmentContents(assignmentPath, filesData);

const http = new HttpClient();
const headers = { 'X-Auth-Key': hexletToken };
const body = { check: { ...checkData, state: checkState } };
const body = { check: { ...checkData, ...assignmentContents, state: checkState } };
const response = await http.postJson(checkCreatePath, body, headers);

// NOTE: любые ответы которые не вызвали падение клиента и не являются успешными - неизвестные
Expand Down
31 changes: 31 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// @ts-check

import _ from 'lodash';
import fse from 'fs-extra';
import fs from 'fs';
import fsp from 'fs/promises';
import path from 'path';

const availableLocales = ['ru'];

Expand All @@ -22,3 +26,30 @@ export const getFullImageName = (namespace, slug, locale, tag) => {

return `${imageName}:${tag}`;
};

export const getFilesData = async (coursePath, lessonSlug) => {
const filesDataPath = path.join(coursePath, 'filesData.json');

if (!fs.existsSync(filesDataPath)) {
return { sourceFiles: [], testFiles: [] };
}

const filesData = await fse.readJSON(filesDataPath);
return filesData[lessonSlug];
};

export const getAssignmentContents = async (assignmentPath, filesData) => {
const getContent = async (filePath) => {
const fullPath = path.join(assignmentPath, filePath);
const content = fs.existsSync(fullPath) ? (await fsp.readFile(fullPath)).toString() : '';
return { [filePath]: content };
};

const sourceContents = await Promise.all(filesData.sourceFiles.map(getContent));
const testContents = await Promise.all(filesData.testFiles.map(getContent));

return {
sourceContents: sourceContents.reduce(_.merge, {}),
testContents: testContents.reduce(_.merge, {}),
};
};

0 comments on commit c472d16

Please sign in to comment.