Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
electric-el committed Mar 8, 2022
2 parents 1cb331a + 0b187c3 commit 936e07a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/mac-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:

- name: Build
run: |
brew update-reset
brew install --cask google-chrome
npm install
npm run lint
npm test
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint scripts && eslint test",
"test": "npm run test:unit",
"test": "npm run test:unit && npm run test:e2e",
"test:e2e": "node test/e2e",
"test:unit": "nyc mocha --exit --require @babel/register test/unit"
},
Expand Down
53 changes: 41 additions & 12 deletions scripts/classes/code-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ export default class CodeViewer {
this.$orgs['#sg-code-console-markdown-log'].getState().html ||
this.$orgs['#sg-code-console-markdown-error'].getState().html
) {
this.$orgs['#sg-code-tab-markdown'].dispatchAction('removeClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-tab-git'].dispatchAction('removeClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-btn-markdown-edit'].dispatchAction('css', {display: ''});
this.$orgs['#sg-code-pane-markdown-console'].dispatchAction('css', {display: ''});
this.$orgs['#sg-code-console-markdown-log'].dispatchAction('html', '');
this.$orgs['#sg-code-console-markdown-error'].dispatchAction('html', '');
this.$orgs['#sg-code-btn-markdown-continue'].dispatchAction('css', {display: ''});
this.$orgs['#sg-code-tab-markdown'].dispatchAction('removeClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-tab-git'].dispatchAction('removeClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-message-git-na'].dispatchAction('html', '');
}

Expand Down Expand Up @@ -306,7 +306,7 @@ export default class CodeViewer {
body
})
.then((response) => {
if (response && response.status === 200) {
if (response && (response.status === 200 || response.status === 403)) {
return response.text();
}
else {
Expand All @@ -326,6 +326,28 @@ export default class CodeViewer {
});
}

gitDiff() {
const gitNaDisplay = this.$orgs['#sg-code-pane-git-na'].getState().css.display;

if (!this.gitInterface || gitNaDisplay === 'block') {
return Promise.resolve();
}
else {
return this.fetchGitCommand(new URLSearchParams('args[0]=diff&rel_path=' + this.markdownSource))
.then((response) => {
return new Promise(
(resolve) => {
if (response) {
resolve(response);
}
else {
resolve({status: 304});
}
});
});
}
}

gitPullMarkdown() {
const gitNaDisplay = this.$orgs['#sg-code-pane-git-na'].getState().css.display;

Expand All @@ -337,6 +359,7 @@ export default class CodeViewer {
.then((response) => {
/* istanbul ignore else */
if (typeof response === 'string') {
this.$orgs['#sg-code-tab-git'].dispatchAction('removeClass', 'sg-code-tab-warning');
// Since we know there are no Git conflicts, reenable Markdown edit button.
this.$orgs['#sg-code-btn-markdown-edit'].dispatchAction('css', {display: ''});
this.$orgs['#sg-code-pane-git-na'].dispatchAction('css', {display: ''});
Expand All @@ -356,8 +379,8 @@ export default class CodeViewer {
}

if (this.gitInterface && err instanceof Object) {
this.$orgs['#sg-code-btn-markdown-edit'].dispatchAction('css', {display: 'none'});
this.$orgs['#sg-code-tab-git'].dispatchAction('addClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-btn-markdown-edit'].dispatchAction('css', {display: 'none'});

if (typeof err.message === 'string') {
this.$orgs['#sg-code-message-git-na'].dispatchAction('html',
Expand All @@ -370,8 +393,8 @@ export default class CodeViewer {
}
}

this.$orgs['#sg-code-pane-git'].dispatchAction('css', {display: ''});
this.$orgs['#sg-code-pane-git-na'].dispatchAction('css', {display: 'block'});
this.$orgs['#sg-code-pane-git'].dispatchAction('css', {display: ''});

return Promise.resolve();
});
Expand Down Expand Up @@ -414,7 +437,8 @@ export default class CodeViewer {

if (this.gitInterface) {
return this.setPanelContent('markdown', this.patternPartial)
.then(() => this.gitPullMarkdown());
.then(() => this.gitPullMarkdown())
.then(() => this.gitDiff());
}
else {
return response.text();
Expand Down Expand Up @@ -442,6 +466,7 @@ export default class CodeViewer {
else if (typeof response === 'object') {
if (response.status === 304) { // Not Modified
this.$orgs['#sg-code-pane-markdown'].dispatchAction('css', {display: 'block'});
this.$orgs['#sg-code-pane-markdown-load-anim'].dispatchAction('css', {display: ''});
}

return Promise.resolve(response);
Expand All @@ -468,8 +493,8 @@ export default class CodeViewer {
const forbiddenClassName = forbidden.getAttribute('class');

forbidden.setAttribute('class', forbiddenClassName + ' sg-code-pane-content-warning');
this.$orgs['#sg-code-pane-markdown-na'].dispatchAction('html', forbidden);
this.$orgs['#sg-code-tab-markdown'].dispatchAction('addClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-pane-markdown-na'].dispatchAction('html', forbidden);

return Promise.resolve(response);
});
Expand Down Expand Up @@ -618,8 +643,10 @@ export default class CodeViewer {
this.markdownSource = null;

forbidden.setAttribute('class', forbiddenClassName + ' sg-code-pane-content-warning');
this.$orgs['#sg-code-pane-markdown-na'].dispatchAction('html', forbidden);
this.$orgs['#sg-code-tab-markdown'].dispatchAction('addClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-pane-markdown-na'].dispatchAction('html', forbidden);
// If Markdown Editor is forbidden, also forbid Git Interface.
this.setPanelContent('git');

return Promise.reject();
}
Expand Down Expand Up @@ -649,11 +676,11 @@ export default class CodeViewer {
});
}
case 'git': {
const gitNaHtml = this.$orgs['#sg-code-message-git-na'].getState().html;
const gitNa = this.$orgs['#sg-code-message-git-na'].getState();

if (
gitNaHtml &&
(gitNaHtml.includes('Command failed: git pull') || gitNaHtml.includes('Command failed: git push'))
gitNa && gitNa.html &&
(gitNa.html.includes('Command failed: git pull') || gitNa.html.includes('Command failed: git push'))
) {
return Promise.resolve();
}
Expand All @@ -679,6 +706,7 @@ export default class CodeViewer {
return Promise.reject(response);
}
else {
this.$orgs['#sg-code-tab-git'].dispatchAction('removeClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-pane-git-na'].dispatchAction('css', {display: ''});
this.$orgs['#sg-code-pane-git'].dispatchAction('css', {display: 'block'});

Expand Down Expand Up @@ -712,6 +740,7 @@ export default class CodeViewer {
const forbiddenClassName = forbidden.getAttribute('class');

forbidden.setAttribute('class', forbiddenClassName + ' sg-code-pane-content-warning');
this.$orgs['#sg-code-tab-git'].dispatchAction('addClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-pane-git-na'].dispatchAction('html', forbidden);
}
else if (typeof rejection === 'string' && rejection.startsWith('fatal:')) {
Expand All @@ -722,8 +751,8 @@ export default class CodeViewer {
this.gitInterface &&
rejection && rejection.message && rejection.message.startsWith('Command failed:')
) {
this.$orgs['#sg-code-btn-markdown-edit'].dispatchAction('css', {display: 'none'});
this.$orgs['#sg-code-tab-git'].dispatchAction('addClass', 'sg-code-tab-warning');
this.$orgs['#sg-code-btn-markdown-edit'].dispatchAction('css', {display: 'none'});
this.$orgs['#sg-code-message-git-na'].dispatchAction('html',
'<pre class="sg-code-pane-content-warning"><code>' + rejection.message + '</code></pre>');
}
Expand Down
8 changes: 8 additions & 0 deletions test/mocks/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ global.fetch = (url, init) => {
}

switch (resolveObj.status) {
case 304: // Not Modified mock.
resolveObj = {
status: 200,
text: () => Promise.resolve('')
};

break;

case 403:
Object.assign(resolveObj, {
json: () => Promise.resolve({message, stack: message}),
Expand Down
51 changes: 51 additions & 0 deletions test/unit/code-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,57 @@ describe('codeViewer', function () {
});
});

describe('.gitDiff()', function () {
before(function () {
codeViewer.gitInterface = true;
codeViewer.markdownSource = '04-pages/00-homepage.md';
});

it('responds with status 304 if the Markdown edit is not different from the last version', function () {
global.mockResponse = {
git_diff_status: 304
};

return codeViewer.gitDiff()
.then((response) => {
expect(response).to.be.an('object');
expect(response.status).to.equal(304);
});
});

it('responds with a non-empty string if the Markdown edit is different from the last version', function () {
global.mockResponse = {
git_diff_status: 200
};

return codeViewer.gitDiff()
.then((response) => {
expect(response).to.be.a('string');
expect(response).to.not.be.empty;
});
});

it('responds with undefined if the GUI shows that Git is not available', function () {
$orgs['#sg-code-pane-git-na'].dispatchAction('css', {display: 'block'});

return codeViewer.gitDiff()
.then((response) => {
expect(response).to.be.undefined;

$orgs['#sg-code-pane-git-na'].dispatchAction('css', {display: ''});
});
});

it('responds with undefined if gitInterface is off', function () {
codeViewer.gitInterface = false;

return codeViewer.gitDiff()
.then((response) => {
expect(response).to.be.undefined;
});
});
});

// Tests .fetchGitCommand()
describe('.revisionAdd()', function () {
const relPath = '04-pages/00-homepage.mustache';
Expand Down
5 changes: 4 additions & 1 deletion test/wdio.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ exports.config = {
// 5 instances get started at a time.
maxInstances: 1,
//
browserName: 'chrome'
browserName: 'chrome',
'goog:chromeOptions': {
args: ['--disable-gpu']
}
// If outputDir is provided WebdriverIO can capture driver session logs
// it is possible to configure which logTypes to include/exclude.
// excludeDriverLogs: ['*'], // pass '*' to exclude all driver session logs
Expand Down

0 comments on commit 936e07a

Please sign in to comment.