Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
annasong20 committed Jan 3, 2023
1 parent b36f0a2 commit b574f8a
Show file tree
Hide file tree
Showing 2 changed files with 385 additions and 23 deletions.
33 changes: 25 additions & 8 deletions api/internal/localizer/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (lc *localizer) localizeNativeFields(kust *types.Kustomization) error {
}{
"resources": {
kust.Resources,
lc.localizePath,
lc.localizeResource,
},
"components": {
kust.Components,
Expand Down Expand Up @@ -247,9 +247,24 @@ func (lc *localizer) localizePatches(patches []types.Patch) error {
return nil
}

// localizePath localizes path, a file or root, and returns the localized path
func (lc *localizer) localizePath(path string) (string, error) {
locPath, fileErr := lc.localizeFile(path)
// localizeResource localizes resource path, a file or root, and returns the
// localized path
func (lc *localizer) localizeResource(path string) (string, error) {
var locPath string

content, fileErr := lc.ldr.Load(path)
// The following check catches the case where path is a repo root.
// Load on a repo will successfully return its README in HTML.
// Because HTML does not follow resource formatting, we then correctly try
// to localize path as a root.
if fileErr == nil {
_, resErr := lc.rFactory.NewResMapFromBytes(content)
if resErr != nil {
fileErr = errors.WrapPrefixf(resErr, "invalid resource at file %q", path)
} else {
locPath, fileErr = lc.localizeFileWithContent(path, content)
}
}
if fileErr != nil {
var rootErr error
locPath, rootErr = lc.localizeDir(path)
Expand Down Expand Up @@ -278,8 +293,9 @@ func (lc *localizer) localizeFile(path string) (string, error) {
func (lc *localizer) localizeFileWithContent(path string, content []byte) (string, error) {
var locPath string
if loader.IsRemoteFile(path) {
// TODO(annasong): You need to check if you can add a localize directory here to store
// the remote file content. There may be a directory that shares the localize directory name.
if lc.fSys.Exists(lc.root.Join(LocalizeDir)) {
return "", errors.Errorf("%s already contains %s needed to store file %q", lc.root, LocalizeDir, path)
}
locPath = locFilePath(path)
} else {
// ldr has checked that path must be relative; this is subject to change in beta.
Expand Down Expand Up @@ -317,8 +333,9 @@ func (lc *localizer) localizeDir(path string) (string, error) {
}
var locPath string
if repo := ldr.Repo(); repo != "" {
// TODO(annasong): You need to check if you can add a localize directory here to store
// the remote file content. There may be a directory that shares the localize directory name.
if lc.fSys.Exists(lc.root.Join(LocalizeDir)) {
return "", errors.Errorf("%s already contains %s needed to store root %q", lc.root, LocalizeDir, path)
}
locPath, err = locRootPath(path, repo, root, lc.fSys)
if err != nil {
return "", err
Expand Down
Loading

0 comments on commit b574f8a

Please sign in to comment.