Skip to content

Commit

Permalink
Refactor starter code and Dockerfile processing
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed Aug 16, 2024
1 parent add7ad4 commit 8d2cc81
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/models/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ export default class Course {
await exec(`rm -rf ${repositoryDir}`);
await exec(`cp -r ${this.compiledStarterRepositoryDirForLanguage(language)} ${repositoryDir}`);

await exec(`git -C ${repositoryDir} init`);
await exec(`git -C ${repositoryDir} add .`);
await exec(`git -C ${repositoryDir} commit -m "Initial commit"`);

// Test runner binary
await exec(`mkdir -p ${repositoryDir}/test-runner`);
await exec(`touch ${repositoryDir}/test-runner/test-runner`);
Expand Down
4 changes: 2 additions & 2 deletions lib/models/dockerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class Dockerfile {

get processedContents(): string {
if (!this._processedContentsCache) {
throw new Error("Not implemented");
throw new Error("processContents was not called!");
}

return this._processedContentsCache;
Expand Down Expand Up @@ -82,7 +82,7 @@ export default class Dockerfile {
throw new Error(`Failed to process Dockerfile. Status: ${response.status}, Response: ${await response.text()}`);
}

const responseBody = await response.json() as { dockerfile_contents: string };
const responseBody = (await response.json()) as { dockerfile_contents: string };
this._processedContentsCache = responseBody.dockerfile_contents;
}
}
1 change: 1 addition & 0 deletions lib/testers/command-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Dockerfile from "../models/dockerfile";

const exec = util.promisify(child_process.exec);

// TODO: Make this work with dockerfile#processedContents
export default class CommandTester extends BaseTester {
course: Course;
dockerfile: Dockerfile;
Expand Down
23 changes: 15 additions & 8 deletions lib/testers/starter-code-tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import StarterCodeUncommenter from "../starter-code-uncommenter";
import LineWithCommentRemover from "../line-with-comment-remover";
import testScriptFile from "../scripts/test.sh";
import ShellCommandExecutor from "../shell-command-executor";
import Dockerfile from "../models/dockerfile";

const writeFile = util.promisify(fs.writeFile);

Expand All @@ -22,6 +23,7 @@ export default class StarterCodeTester extends BaseTester {
course: Course;
language: Language;
testerDir: string;
_dockerfile?: Dockerfile;

copiedStarterDir: string | undefined;

Expand All @@ -33,12 +35,8 @@ export default class StarterCodeTester extends BaseTester {
}

async doTest() {
this.copiedStarterDir = tmp.dirSync().name;
await exec(`rm -rf ${this.copiedStarterDir}`);
await exec(`cp -r ${this.starterDir} ${this.copiedStarterDir}`);
await exec(`git -C ${this.copiedStarterDir} init`);
await exec(`git -C ${this.copiedStarterDir} add .`);
await exec(`git -C ${this.copiedStarterDir} commit -m "Initial commit"`);
this.copiedStarterDir = await this.course.prepareRepositoryDirForLanguage(this.language);
await this.dockerfile!.processContents();

Logger.logHeader(`Testing starter: ${this.course.slug}-${this.language.slug}`);

Expand Down Expand Up @@ -90,6 +88,10 @@ export default class StarterCodeTester extends BaseTester {
Logger.logInfo("Restoring changes to .sh files");
await ShellCommandExecutor.execute(`git -C ${this.copiedStarterDir} restore *.sh`); // Hack to work around our precompilation step mangling .sh files

Logger.logInfo("Removing test-runner & tester"); // We use this for the tester directories
await ShellCommandExecutor.execute(`rm -rf ${this.copiedStarterDir}/test-runner`);
await ShellCommandExecutor.execute(`rm -rf ${this.copiedStarterDir}/tester`);

const diff = await ShellCommandExecutor.execute(`git -C ${this.copiedStarterDir} diff --exit-code`, { expectedExitCodes: [0, 1] });

if (diff.exitCode === 0) {
Expand Down Expand Up @@ -154,8 +156,13 @@ export default class StarterCodeTester extends BaseTester {
Logger.logSuccess(`Took ${timeTaken} secs`);
}

// Cache so that Dockerfile.processedContents is stored
get dockerfile() {
return this.course.latestDockerfiles.find((dockerfile) => dockerfile.languagePack === this.languagePack);
if (!this._dockerfile) {
this._dockerfile = this.course.latestDockerfiles.find((dockerfile) => dockerfile.languagePack === this.languagePack);
}

return this._dockerfile;
}

get slug() {
Expand All @@ -171,7 +178,7 @@ export default class StarterCodeTester extends BaseTester {
}

async buildImage() {
const command = `docker build -t ${this.slug} -f ${this.dockerfile!.path} ${this.copiedStarterDir}`;
const command = `docker build -t ${this.slug} -f ${this.dockerfile!.processedPath} ${this.copiedStarterDir}`;
const expectedOutput = `naming to docker.io/library/${this.slug}`;
await this.assertStderrContains(command, expectedOutput);
}
Expand Down

0 comments on commit 8d2cc81

Please sign in to comment.