Skip to content

Commit

Permalink
test(up): test reloading of server upon git change
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 8, 2022
1 parent e751e95 commit f9a6bfc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
62 changes: 61 additions & 1 deletion test/up-cmd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import assert from 'assert';
import path from 'path';
import fse from 'fs-extra';
import {
Nock, assertHttp, createTestRoot, initGit,
Nock, assertHttp, createTestRoot, initGit, switchBranch,
} from './utils.js';
import UpCommand from '../src/up.cmd.js';

Expand Down Expand Up @@ -176,6 +176,66 @@ describe('Integration test for up command with helix pages', function suite() {
.run()
.catch(done);
});

it('up command reloads when git branch changes.', (done) => {
initGit(testDir, 'https://github.com/adobe/dummy-foo.git');
let error = null;
const cmd = new UpCommand()
.withLiveReload(false)
.withDirectory(testDir)
.withOpen(false)
.withHttpPort(0);

const myDone = (err) => {
error = err;
return cmd.stop();
};

nock('https://main--dummy-foo--adobe.hlx.page')
.get('/index.html')
.reply(200, '## Welcome')
.get('/not-found.txt')
.reply(404)
.get('/head.html')
.reply(200, '<link rel="stylesheet" href="/styles.css"/>');

nock('https://raw.githubusercontent.com')
.get('/adobe/dummy-foo/master/fstab.yaml')
.reply(404, 'dummy');

nock('https://raw.githubusercontent.com')
.get('/adobe/dummy-foo/new-branch/fstab.yaml')
.reply(200, 'yep!');

nock('https://new-branch--dummy-foo--adobe.hlx.page')
.get('/head.html')
.reply(200, '## Welcome');

cmd
.on('started', async () => {
try {
let ret = await assertHttp(`http://localhost:${cmd.project.server.port}/index.html`, 200);
assert.strictEqual(ret.trim(), '## Welcome');
ret = await assertHttp(`http://localhost:${cmd.project.server.port}/local.txt`, 200);
assert.strictEqual(ret.trim(), 'Hello, world.');
await assertHttp(`http://localhost:${cmd.project.server.port}/not-found.txt`, 404);
// now switch to a new branch
switchBranch(testDir, 'new-branch');
// wait 1 second for the git branch to be detected
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
await myDone();
} catch (e) {
await myDone(e);
}
})
.on('stopped', () => {
done(error);
})
.run()
.catch(done);
});
});

describe('Integration test for up command with cache', function suite() {
Expand Down
8 changes: 8 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export function initGit(dir, remote, branch) {
shell.cd(pwd);
}

export function switchBranch(dir, branch) {
const pwd = shell.pwd();
shell.cd(dir);
shell.exec(`git checkout -b ${branch}`);
shell.cd(pwd);
console.log(`switched to branch ${branch} in ${dir}`);
}

export function clearHelixEnv() {
const deleted = {};
Object.keys(process.env).filter((key) => key.startsWith('HLX_')).forEach((key) => {
Expand Down

0 comments on commit f9a6bfc

Please sign in to comment.