Skip to content

Commit

Permalink
Fix #39 Add code to consider another case
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <remy.suen@gmail.com>
  • Loading branch information
rcjsuen committed Jun 4, 2018
1 parent 86bccdf commit b6d0390
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/instructions/from.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export class From extends Instruction {
public getImageTagRange(): Range | null {
const range = this.getImageRange();
if (range) {
let content = this.getRangeContent(range);
if (content.indexOf('@') === -1) {
if (this.getImageDigestRange() === null) {
let content = this.getRangeContent(range);
let index = this.lastIndexOf(this.document.offsetAt(range.start), content, ':');
// the colon might be for a private registry's port and not a tag
if (index > content.indexOf('/')) {
Expand Down Expand Up @@ -120,9 +120,9 @@ export class From extends Instruction {
let index = content.indexOf(searchString);
const variables = this.getVariables();
for (let i = 0; i < variables.length; i++) {
const position = this.document.positionAt(documentOffset + index);
const position = documentOffset + index;
const variableRange = variables[i].getRange();
if (Util.isInsideRange(position, variableRange)) {
if (this.document.offsetAt(variableRange.start) < position && position < this.document.offsetAt(variableRange.end)) {
const offset = this.document.offsetAt(variableRange.end) - documentOffset;
const substring = content.substring(offset);
const subIndex = substring.indexOf(searchString);
Expand All @@ -141,8 +141,9 @@ export class From extends Instruction {
let index = content.lastIndexOf(searchString);
const variables = this.getVariables();
for (let i = 0; i < variables.length; i++) {
const position = this.document.positionAt(documentOffset + index);
if (Util.isInsideRange(position, variables[i].getRange())) {
const position = documentOffset + index;
const variableRange = variables[i].getRange();
if (this.document.offsetAt(variableRange.start) < position && position < this.document.offsetAt(variableRange.end)) {
index = content.substring(0, index).lastIndexOf(searchString);
if (index === -1) {
return -1;
Expand Down
16 changes: 16 additions & 0 deletions test/instructions/from.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ describe("FROM", () => {
assert.equal(from.getBuildStage(), null);
});

it("FROM ${image:-'@blah'}:3.6", () => {
let dockerfile = DockerfileParser.parse("FROM ${image:-'@blah'}:3.6");
let from = dockerfile.getFROMs()[0];
assert.equal(from.getImage(), "${image:-'@blah'}:3.6");
assertRange(from.getImageRange(), 0, 5, 0, 26);
assert.equal(from.getImageName(), "${image:-'@blah'}");
assertRange(from.getImageNameRange(), 0, 5, 0, 22);
assert.equal(from.getImageTag(), "3.6");
assertRange(from.getImageTagRange(), 0, 23, 0, 26);
assert.equal(from.getImageDigest(), null);
assert.equal(from.getImageDigestRange(), null);
assert.equal(from.getRegistry(), null);
assert.equal(from.getRegistryRange(), null);
assert.equal(from.getBuildStage(), null);
});

it("FROM custom/no${de:-'//'}", () => {
let dockerfile = DockerfileParser.parse("FROM custom/no${de:-'//'}");
let from = dockerfile.getFROMs()[0];
Expand Down

0 comments on commit b6d0390

Please sign in to comment.