Skip to content

Commit

Permalink
Merge pull request #21 from smartrecruiters/LGD-134_named_stages
Browse files Browse the repository at this point in the history
Trim "AS name" from the FROM clause
  • Loading branch information
kasinskim authored Jun 4, 2024
2 parents f128f34 + be3d560 commit 2fd8bae
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bakery/service/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (dip *dockerImageParser) ParseDockerfile(dockerfilePath string) (*DockerIma
return nil, fmt.Errorf("unable to extract dependency from %s file. Check if first line starts with `FROM `", dockerfilePath)
}

// keep only "FROM <image-name>" part of the line, so "AS <stage-name>" is removed
line = strings.Join(strings.Fields(line)[0:2], " ")

dependsOnLong := strings.TrimPrefix(line, dependencyPrefix)
dependsOnShort := dependsOnLong
dependsOnVersion := ""
Expand Down
41 changes: 41 additions & 0 deletions bakery/service/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package service

import (
"testing"

"github.com/stretchr/testify/assert"
)

func copyDockerImgObjectForCompare(dockerImg *DockerImage) *DockerImage {
return &DockerImage{
Name: dockerImg.Name,
DockerfilePath: "dummy",
DockerfileDir: dockerImg.DockerfileDir,
DependsOnLong: dockerImg.DependsOnLong,
DependsOnShort: dockerImg.DependsOnShort,
DependsOnVersion: dockerImg.DependsOnVersion,
nextVersion: dockerImg.nextVersion,
latestVersion: dockerImg.latestVersion,
}
}

func TestParserDependenciesFromFromClause(t *testing.T) {
dockerImgParser := NewDockerImageParser()

dockerImgUnnamed, _ := dockerImgParser.ParseDockerfile("testcases/Dockerfile1")
dockerImgUnnamedCopy := copyDockerImgObjectForCompare(dockerImgUnnamed)

dockerImgNamedUppercase, _ := dockerImgParser.ParseDockerfile("testcases/Dockerfile2")
dockerImgNamedUppercaseCopy := copyDockerImgObjectForCompare(dockerImgNamedUppercase)

dockerImgNamedLowercase, _ := dockerImgParser.ParseDockerfile("testcases/Dockerfile3")
dockerImgNamedLowercaseCopy := copyDockerImgObjectForCompare(dockerImgNamedLowercase)

assert.Equal(t, dockerImgUnnamed.DependsOnLong, "ubuntu:2204")
assert.Equal(t, dockerImgUnnamed.DependsOnShort, "ubuntu")
assert.Equal(t, dockerImgUnnamed.DependsOnVersion, "2204")

assert.Equal(t, dockerImgUnnamedCopy, dockerImgNamedUppercaseCopy)
assert.Equal(t, dockerImgUnnamedCopy, dockerImgNamedLowercaseCopy)
assert.Equal(t, dockerImgNamedUppercaseCopy, dockerImgNamedLowercaseCopy)
}
1 change: 1 addition & 0 deletions bakery/service/testcases/Dockerfile1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM ubuntu:2204
1 change: 1 addition & 0 deletions bakery/service/testcases/Dockerfile2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM ubuntu:2204 AS base
1 change: 1 addition & 0 deletions bakery/service/testcases/Dockerfile3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM ubuntu:2204 as base

0 comments on commit 2fd8bae

Please sign in to comment.