Merge "Harden gr-formatted-text agains slow/failed project configs"

This commit is contained in:
Kasper Nilsson
2017-08-02 20:38:32 +00:00
committed by Gerrit Code Review
17 changed files with 71 additions and 34 deletions

View File

@@ -338,6 +338,7 @@ limitations under the License.
patch-range="[[patchRange]]" patch-range="[[patchRange]]"
path="[[file.__path]]" path="[[file.__path]]"
prefs="[[diffPrefs]]" prefs="[[diffPrefs]]"
project-name="[[change.project]]"
project-config="[[projectConfig]]" project-config="[[projectConfig]]"
on-line-selected="_onLineSelected" on-line-selected="_onLineSelected"
no-render-on-prefs-change no-render-on-prefs-change

View File

@@ -19,9 +19,10 @@
const IMAGE_MIME_PATTERN = /^image\/(bmp|gif|jpeg|jpg|png|tiff|webp)$/; const IMAGE_MIME_PATTERN = /^image\/(bmp|gif|jpeg|jpg|png|tiff|webp)$/;
function GrDiffBuilderImage(diff, comments, prefs, outputEl, baseImage, function GrDiffBuilderImage(
revisionImage) { diff, comments, prefs, projectName, outputEl, baseImage, revisionImage) {
GrDiffBuilderSideBySide.call(this, diff, comments, prefs, outputEl, []); GrDiffBuilderSideBySide.call(
this, diff, comments, prefs, projectName, outputEl, []);
this._baseImage = baseImage; this._baseImage = baseImage;
this._revisionImage = revisionImage; this._revisionImage = revisionImage;
} }

View File

@@ -17,8 +17,10 @@
// Prevent redefinition. // Prevent redefinition.
if (window.GrDiffBuilderSideBySide) { return; } if (window.GrDiffBuilderSideBySide) { return; }
function GrDiffBuilderSideBySide(diff, comments, prefs, outputEl, layers) { function GrDiffBuilderSideBySide(
GrDiffBuilder.call(this, diff, comments, prefs, outputEl, layers); diff, comments, prefs, projectName, outputEl, layers) {
GrDiffBuilder.call(
this, diff, comments, prefs, projectName, outputEl, layers);
} }
GrDiffBuilderSideBySide.prototype = Object.create(GrDiffBuilder.prototype); GrDiffBuilderSideBySide.prototype = Object.create(GrDiffBuilder.prototype);
GrDiffBuilderSideBySide.prototype.constructor = GrDiffBuilderSideBySide; GrDiffBuilderSideBySide.prototype.constructor = GrDiffBuilderSideBySide;

View File

@@ -17,8 +17,10 @@
// Prevent redefinition. // Prevent redefinition.
if (window.GrDiffBuilderUnified) { return; } if (window.GrDiffBuilderUnified) { return; }
function GrDiffBuilderUnified(diff, comments, prefs, outputEl, layers) { function GrDiffBuilderUnified(
GrDiffBuilder.call(this, diff, comments, prefs, outputEl, layers); diff, comments, prefs, projectName, outputEl, layers) {
GrDiffBuilder.call(
this, diff, comments, prefs, projectName, outputEl, layers);
} }
GrDiffBuilderUnified.prototype = Object.create(GrDiffBuilder.prototype); GrDiffBuilderUnified.prototype = Object.create(GrDiffBuilder.prototype);
GrDiffBuilderUnified.prototype.constructor = GrDiffBuilderUnified; GrDiffBuilderUnified.prototype.constructor = GrDiffBuilderUnified;

View File

@@ -94,6 +94,7 @@ limitations under the License.
isImageDiff: Boolean, isImageDiff: Boolean,
baseImage: Object, baseImage: Object,
revisionImage: Object, revisionImage: Object,
projectName: String,
_builder: Object, _builder: Object,
_groups: Array, _groups: Array,
_layers: Array, _layers: Array,
@@ -219,9 +220,9 @@ limitations under the License.
}, },
createCommentThreadGroup(changeNum, patchNum, path, createCommentThreadGroup(changeNum, patchNum, path,
isOnParent, commentSide, projectConfig) { isOnParent, commentSide) {
return this._builder.createCommentThreadGroup(changeNum, patchNum, return this._builder.createCommentThreadGroup(changeNum, patchNum,
path, isOnParent, commentSide, projectConfig); path, isOnParent, commentSide);
}, },
emitGroup(group, sectionEl) { emitGroup(group, sectionEl) {
@@ -252,13 +253,14 @@ limitations under the License.
_getDiffBuilder(diff, comments, prefs) { _getDiffBuilder(diff, comments, prefs) {
if (this.isImageDiff) { if (this.isImageDiff) {
return new GrDiffBuilderImage(diff, comments, prefs, return new GrDiffBuilderImage(diff, comments, prefs,
this.diffElement, this.baseImage, this.revisionImage); this.projectName, this.diffElement, this.baseImage,
this.revisionImage);
} else if (this.viewMode === DiffViewMode.SIDE_BY_SIDE) { } else if (this.viewMode === DiffViewMode.SIDE_BY_SIDE) {
return new GrDiffBuilderSideBySide( return new GrDiffBuilderSideBySide(diff, comments, prefs,
diff, comments, prefs, this.diffElement, this._layers); this.projectName, this.diffElement, this._layers);
} else if (this.viewMode === DiffViewMode.UNIFIED) { } else if (this.viewMode === DiffViewMode.UNIFIED) {
return new GrDiffBuilderUnified( return new GrDiffBuilderUnified(diff, comments, prefs,
diff, comments, prefs, this.diffElement, this._layers); this.projectName, this.diffElement, this._layers);
} }
throw Error('Unsupported diff view mode: ' + this.viewMode); throw Error('Unsupported diff view mode: ' + this.viewMode);
}, },

View File

@@ -30,10 +30,11 @@
const REGEX_ASTRAL_SYMBOL = /[\uD800-\uDBFF][\uDC00-\uDFFF]/; const REGEX_ASTRAL_SYMBOL = /[\uD800-\uDBFF][\uDC00-\uDFFF]/;
function GrDiffBuilder(diff, comments, prefs, outputEl, layers) { function GrDiffBuilder(diff, comments, prefs, projectName, outputEl, layers) {
this._diff = diff; this._diff = diff;
this._comments = comments; this._comments = comments;
this._prefs = prefs; this._prefs = prefs;
this._projectName = projectName;
this._outputEl = outputEl; this._outputEl = outputEl;
this.groups = []; this.groups = [];
@@ -336,14 +337,14 @@
}; };
GrDiffBuilder.prototype.createCommentThreadGroup = function(changeNum, GrDiffBuilder.prototype.createCommentThreadGroup = function(changeNum,
patchNum, path, isOnParent, projectConfig, range) { patchNum, path, isOnParent, range) {
const threadGroupEl = const threadGroupEl =
document.createElement('gr-diff-comment-thread-group'); document.createElement('gr-diff-comment-thread-group');
threadGroupEl.changeNum = changeNum; threadGroupEl.changeNum = changeNum;
threadGroupEl.patchForNewThreads = patchNum; threadGroupEl.patchForNewThreads = patchNum;
threadGroupEl.path = path; threadGroupEl.path = path;
threadGroupEl.isOnParent = isOnParent; threadGroupEl.isOnParent = isOnParent;
threadGroupEl.projectConfig = projectConfig; threadGroupEl.projectName = this._projectName;
threadGroupEl.range = range; threadGroupEl.range = range;
return threadGroupEl; return threadGroupEl;
}; };
@@ -370,8 +371,7 @@
this._comments.meta.changeNum, this._comments.meta.changeNum,
patchNum, patchNum,
this._comments.meta.path, this._comments.meta.path,
isOnParent, isOnParent);
this._comments.meta.projectConfig);
threadGroupEl.comments = comments; threadGroupEl.comments = comments;
if (opt_side) { if (opt_side) {
threadGroupEl.setAttribute('data-side', opt_side); threadGroupEl.setAttribute('data-side', opt_side);

View File

@@ -62,13 +62,16 @@ limitations under the License.
setup(() => { setup(() => {
stub('gr-rest-api-interface', { stub('gr-rest-api-interface', {
getLoggedIn() { return Promise.resolve(false); }, getLoggedIn() { return Promise.resolve(false); },
getProjectConfig() { return Promise.resolve({}); },
}); });
const prefs = { const prefs = {
line_length: 10, line_length: 10,
show_tabs: true, show_tabs: true,
tab_size: 4, tab_size: 4,
}; };
builder = new GrDiffBuilder({content: []}, {left: [], right: []}, prefs); const projectName = 'my-project';
builder = new GrDiffBuilder(
{content: []}, {left: [], right: []}, prefs, projectName);
}); });
test('context control buttons', () => { test('context control buttons', () => {
@@ -265,7 +268,7 @@ limitations under the License.
assert.equal(threadGroupEl.patchForNewThreads, patchNum); assert.equal(threadGroupEl.patchForNewThreads, patchNum);
assert.equal(threadGroupEl.path, '/path/to/foo'); assert.equal(threadGroupEl.path, '/path/to/foo');
assert.equal(threadGroupEl.isOnParent, isOnParent); assert.equal(threadGroupEl.isOnParent, isOnParent);
assert.deepEqual(threadGroupEl.projectConfig, {foo: 'bar'}); assert.deepEqual(threadGroupEl.projectName, 'my-project');
assert.deepEqual(threadGroupEl.comments, comments); assert.deepEqual(threadGroupEl.comments, comments);
} }
@@ -761,7 +764,7 @@ limitations under the License.
outputEl = element.queryEffectiveChildren('#diffTable'); outputEl = element.queryEffectiveChildren('#diffTable');
sandbox.stub(element, '_getDiffBuilder', () => { sandbox.stub(element, '_getDiffBuilder', () => {
const builder = new GrDiffBuilder( const builder = new GrDiffBuilder(
{content}, {left: [], right: []}, prefs, outputEl); {content}, {left: [], right: []}, prefs, 'my-project', outputEl);
sandbox.stub(builder, 'addColumns'); sandbox.stub(builder, 'addColumns');
builder.buildSectionElement = function(group) { builder.buildSectionElement = function(group) {
const section = document.createElement('stub'); const section = document.createElement('stub');

View File

@@ -38,7 +38,7 @@ limitations under the License.
location-range="[[thread.locationRange]]" location-range="[[thread.locationRange]]"
patch-num="[[thread.patchNum]]" patch-num="[[thread.patchNum]]"
path="[[path]]" path="[[path]]"
project-config="[[projectConfig]]"></gr-diff-comment-thread> project-name="[[projectName]]"></gr-diff-comment-thread>
</template> </template>
</template> </template>
<script src="gr-diff-comment-thread-group.js"></script> <script src="gr-diff-comment-thread-group.js"></script>

View File

@@ -23,8 +23,8 @@
type: Array, type: Array,
value() { return []; }, value() { return []; },
}, },
projectName: String,
patchForNewThreads: String, patchForNewThreads: String,
projectConfig: Object,
range: Object, range: Object,
isOnParent: { isOnParent: {
type: Boolean, type: Boolean,

View File

@@ -61,7 +61,7 @@ limitations under the License.
show-actions="[[_showActions]]" show-actions="[[_showActions]]"
comment-side="[[comment.__commentSide]]" comment-side="[[comment.__commentSide]]"
side="[[comment.side]]" side="[[comment.side]]"
project-config="[[projectConfig]]" project-config="[[_projectConfig]]"
on-create-fix-comment="_handleCommentFix" on-create-fix-comment="_handleCommentFix"
on-comment-discard="_handleCommentDiscard"></gr-diff-comment> on-comment-discard="_handleCommentDiscard"></gr-diff-comment>
</template> </template>

View File

@@ -40,7 +40,10 @@
commentSide: String, commentSide: String,
patchNum: String, patchNum: String,
path: String, path: String,
projectConfig: Object, projectName: {
type: String,
observer: '_projectNameChanged',
},
isOnParent: { isOnParent: {
type: Boolean, type: Boolean,
value: false, value: false,
@@ -53,6 +56,7 @@
type: Boolean, type: Boolean,
notify: true, notify: true,
}, },
_projectConfig: Object,
}, },
behaviors: [ behaviors: [
@@ -352,5 +356,16 @@
_computeHostClass(unresolved) { _computeHostClass(unresolved) {
return unresolved ? 'unresolved' : ''; return unresolved ? 'unresolved' : '';
}, },
/**
* Load the project config when a project name has been provided.
* @param {string} name The project name.
*/
_projectNameChanged(name) {
if (!name) { return; }
this.$.restAPI.getProjectConfig(name).then(config => {
this._projectConfig = config;
});
},
}); });
})(); })();

View File

@@ -161,6 +161,17 @@ limitations under the License.
lastComment.__draft = true; lastComment.__draft = true;
assert.equal(element._hideActions(showActions, lastComment), true); assert.equal(element._hideActions(showActions, lastComment), true);
}); });
test('setting project name loads the project config', done => {
const projectName = 'foo/bar/baz';
const getProjectStub = sandbox.stub(element.$.restAPI, 'getProjectConfig')
.returns(Promise.resolve({}));
element.projectName = projectName;
flush(() => {
assert.isTrue(getProjectStub.calledWithExactly(projectName));
done();
});
});
}); });
suite('comment action tests', () => { suite('comment action tests', () => {

View File

@@ -328,6 +328,7 @@ limitations under the License.
path="[[_path]]" path="[[_path]]"
prefs="[[_prefs]]" prefs="[[_prefs]]"
project-config="[[_projectConfig]]" project-config="[[_projectConfig]]"
project-name="[[_change.project]]"
view-mode="[[_diffMode]]" view-mode="[[_diffMode]]"
on-line-selected="_onLineSelected"> on-line-selected="_onLineSelected">
</gr-diff> </gr-diff>

View File

@@ -229,6 +229,7 @@ limitations under the License.
<gr-diff-builder <gr-diff-builder
id="diffBuilder" id="diffBuilder"
comments="[[comments]]" comments="[[comments]]"
project-name="[[projectName]]"
diff="[[_diff]]" diff="[[_diff]]"
diff-path="[[path]]" diff-path="[[path]]"
view-mode="[[viewMode]]" view-mode="[[viewMode]]"

View File

@@ -58,6 +58,7 @@
type: Object, type: Object,
observer: '_projectConfigChanged', observer: '_projectConfigChanged',
}, },
projectName: String,
displayLine: { displayLine: {
type: Boolean, type: Boolean,
value: false, value: false,
@@ -305,8 +306,7 @@
let threadGroupEl = this._getThreadGroupForLine(contentEl); let threadGroupEl = this._getThreadGroupForLine(contentEl);
if (!threadGroupEl) { if (!threadGroupEl) {
threadGroupEl = this.$.diffBuilder.createCommentThreadGroup( threadGroupEl = this.$.diffBuilder.createCommentThreadGroup(
this.changeNum, patchNum, this.path, isOnParent, this.changeNum, patchNum, this.path, isOnParent);
this.projectConfig);
contentEl.appendChild(threadGroupEl); contentEl.appendChild(threadGroupEl);
} }

View File

@@ -60,7 +60,7 @@
// request for it still being in flight), set the content anyway to // request for it still being in flight), set the content anyway to
// prevent waiting on the config to display the text. // prevent waiting on the config to display the text.
if (this.config) { return; } if (this.config) { return; }
this.$.container.textContent = content; this._contentOrConfigChanged(content);
}, },
/** /**

View File

@@ -363,12 +363,10 @@ limitations under the License.
assert.equal(result, expected); assert.equal(result, expected);
}); });
test('_contentOrConfigChanged not called without config', () => { test('_computeNodes called without config', () => {
const contentStub = sandbox.stub(element, '_contentChanged'); const computeNodesSpy = sandbox.spy(element, '_computeNodes');
const contentConfigStub = sandbox.stub(element, '_contentOrConfigChanged');
element.content = 'some text'; element.content = 'some text';
assert.isTrue(contentStub.called); assert.isTrue(computeNodesSpy.called);
assert.isFalse(contentConfigStub.called);
}); });
test('_contentOrConfigChanged called with config', () => { test('_contentOrConfigChanged called with config', () => {