Skip to content

Commit

Permalink
Add test and trailing new line to apko.lock.json files.
Browse files Browse the repository at this point in the history
- There was no test that `apko lock' command works
- Some code-quality tools like end-of-file-fixer were annoyed thay .apko.lock.files are not ending with a new-line character.

Signed-off-by: Piotr Tabor <piotr.tabor@snowflake.com>
  • Loading branch information
sfc-gh-ptabor committed Jan 18, 2024
1 parent c10dc55 commit 37eae4b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/cli/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func lockInternal(cmdName string, extension string, deprecated string) *cobra.Co

archs := types.ParseArchitectures(archstrs)

return ResolveCmd(
return LockCmd(
cmd.Context(),
output,
archs,
Expand All @@ -86,7 +86,7 @@ func lockInternal(cmdName string, extension string, deprecated string) *cobra.Co
return cmd
}

func ResolveCmd(ctx context.Context, output string, archs []types.Architecture, opts []build.Option) error {
func LockCmd(ctx context.Context, output string, archs []types.Architecture, opts []build.Option) error {
log := clog.FromContext(ctx)
wd, err := os.MkdirTemp("", "apko-*")
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions internal/cli/lock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli_test

import (
"bytes"
"context"
"os"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"chainguard.dev/apko/internal/cli"
"chainguard.dev/apko/pkg/build"
"chainguard.dev/apko/pkg/build/types"
)

func TestLock(t *testing.T) {
ctx := context.Background()
tmp := t.TempDir()

golden := filepath.Join("testdata", "apko.lock.json")

config := filepath.Join("testdata", "apko.yaml")
archs := types.ParseArchitectures([]string{"amd64", "arm64"})
opts := []build.Option{build.WithConfig(config)}
outputPath := filepath.Join(tmp, "apko.lock.json")

err := cli.LockCmd(ctx, outputPath, archs, opts)
require.NoError(t, err)

want, err := os.ReadFile(golden)
require.NoError(t, err)
got, err := os.ReadFile(outputPath)
require.NoError(t, err)

if !bytes.Equal(want, got) {
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Mismatched lock files: (-%q +%q):\n%s", golden, outputPath, diff)
}
}
}
2 changes: 1 addition & 1 deletion internal/cli/testdata/apko.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@
}
]
}
}
}
4 changes: 3 additions & 1 deletion pkg/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (lock Lock) SaveToFile(lockFile string) error {
if err != nil {
return fmt.Errorf("failed to marshall json: %w", err)
}

// Github and pre-commit checks (like end-of-file-fixer) are expecting ASCII files
// to end with a newline that marshal is not providing.
jsonb = append(jsonb, '\n')
return os.WriteFile(lockFile, jsonb, os.ModePerm)
}

0 comments on commit 37eae4b

Please sign in to comment.