Skip to content

Commit

Permalink
Merge pull request #5486 from snyk/fix/CLI-509_delete_tmp
Browse files Browse the repository at this point in the history
fix: use runtimeInfo to derive the version for cliv1 path
  • Loading branch information
PeterSchafer authored Sep 24, 2024
2 parents 42a1159 + 652d1ba commit 0a6a560
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 49 deletions.
9 changes: 3 additions & 6 deletions cliv2/internal/cliv2/cliv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/rs/zerolog"
"github.com/snyk/go-application-framework/pkg/configuration"
"github.com/snyk/go-application-framework/pkg/instrumentation"
"github.com/snyk/go-application-framework/pkg/runtimeinfo"
"github.com/snyk/go-application-framework/pkg/utils"

cli_errors "github.com/snyk/cli/cliv2/internal/errors"
Expand Down Expand Up @@ -57,14 +58,10 @@ const (
V2_ABOUT Handler = iota
)

func NewCLIv2(config configuration.Configuration, debugLogger *log.Logger) (*CLI, error) {
func NewCLIv2(config configuration.Configuration, debugLogger *log.Logger, ri runtimeinfo.RuntimeInfo) (*CLI, error) {
cacheDirectory := config.GetString(configuration.CACHE_PATH)

v1BinaryLocation, err := cliv1.GetFullCLIV1TargetPath(cacheDirectory)
if err != nil {
fmt.Println(err)
return nil, err
}
v1BinaryLocation := path.Join(cacheDirectory, ri.GetVersion(), cliv1.GetCLIv1Filename())

cli := CLI{
DebugLogger: debugLogger,
Expand Down
23 changes: 15 additions & 8 deletions cliv2/internal/cliv2/cliv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (

"github.com/snyk/go-application-framework/pkg/app"
"github.com/snyk/go-application-framework/pkg/configuration"
"github.com/snyk/go-application-framework/pkg/runtimeinfo"
"github.com/snyk/go-application-framework/pkg/utils"

"github.com/snyk/cli/cliv2/internal/embedded/cliv1"
cli_errors "github.com/snyk/cli/cliv2/internal/errors"

"github.com/snyk/cli/cliv2/internal/cliv2"
Expand All @@ -36,6 +38,11 @@ func getCacheDir(t *testing.T) string {
return cacheDir
}

func getRuntimeInfo(t *testing.T) runtimeinfo.RuntimeInfo {
t.Helper()
return runtimeinfo.New(runtimeinfo.WithVersion(cliv1.CLIV1Version()))
}

func Test_PrepareV1EnvironmentVariables_Fill_and_Filter(t *testing.T) {
orgid := "orgid"
testapi := "https://api.snyky.io"
Expand Down Expand Up @@ -270,7 +277,7 @@ func Test_prepareV1Command(t *testing.T) {
cacheDir := getCacheDir(t)
config := configuration.NewInMemory()
config.Set(configuration.CACHE_PATH, cacheDir)
cli, err := cliv2.NewCLIv2(config, discardLogger)
cli, err := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.NoError(t, err)

snykCmd, err := cli.PrepareV1Command(
Expand Down Expand Up @@ -299,7 +306,7 @@ func Test_extractOnlyOnce(t *testing.T) {
assert.NoDirExists(t, tmpDir)

// create instance under test
cli, err := cliv2.NewCLIv2(config, discardLogger)
cli, err := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.NoError(t, err)
assert.NoError(t, cli.Init())

Expand Down Expand Up @@ -333,7 +340,7 @@ func Test_init_extractDueToInvalidBinary(t *testing.T) {
assert.NoDirExists(t, tmpDir)

// create instance under test
cli, err := cliv2.NewCLIv2(config, discardLogger)
cli, err := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.NoError(t, err)

// fill binary with invalid data
Expand Down Expand Up @@ -371,7 +378,7 @@ func Test_executeRunV2only(t *testing.T) {
assert.NoDirExists(t, tmpDir)

// create instance under test
cli, err := cliv2.NewCLIv2(config, discardLogger)
cli, err := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.NoError(t, err)
assert.NoError(t, cli.Init())

Expand All @@ -388,7 +395,7 @@ func Test_executeUnknownCommand(t *testing.T) {
config.Set(configuration.CACHE_PATH, cacheDir)

// create instance under test
cli, err := cliv2.NewCLIv2(config, discardLogger)
cli, err := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.NoError(t, err)
assert.NoError(t, cli.Init())

Expand All @@ -403,7 +410,7 @@ func Test_clearCache(t *testing.T) {
config.Set(configuration.CACHE_PATH, cacheDir)

// create instance under test
cli, _ := cliv2.NewCLIv2(config, discardLogger)
cli, _ := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.Nil(t, cli.Init())

// create folders and files in cache dir
Expand Down Expand Up @@ -439,7 +446,7 @@ func Test_clearCacheBigCache(t *testing.T) {
config.Set(configuration.CACHE_PATH, cacheDir)

// create instance under test
cli, err := cliv2.NewCLIv2(config, discardLogger)
cli, err := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.NoError(t, err)
assert.NoError(t, cli.Init())

Expand Down Expand Up @@ -480,7 +487,7 @@ func Test_setTimeout(t *testing.T) {
t.Skip("Skipping test on windows")
}
config := configuration.NewInMemory()
cli, err := cliv2.NewCLIv2(config, discardLogger)
cli, err := cliv2.NewCLIv2(config, discardLogger, getRuntimeInfo(t))
assert.NoError(t, err)
config.Set(configuration.TIMEOUT, 1)

Expand Down
11 changes: 0 additions & 11 deletions cliv2/internal/embedded/cliv1/cliv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package cliv1

import (
_ "embed"
"path"
"strings"

"github.com/snyk/cli/cliv2/internal/embedded"
"github.com/snyk/cli/cliv2/internal/utils"
)

// The actual version gets injected at build time
Expand All @@ -16,15 +14,6 @@ func CLIV1Version() string {
return strings.TrimSpace(snykCLIVersion)
}

// Get the full path to where we expect the CLIv1 to be in the cache
// If it doesn't exist, this is the path where we will then extract it
func GetFullCLIV1TargetPath(cacheDir string) (string, error) {
cliv1Filename := getCLIv1Filename()
versionTag := CLIV1Version()
fullPath := path.Join(utils.GetVersionCacheDirectory(cacheDir, versionTag), cliv1Filename)
return fullPath, nil
}

func ExtractTo(targetFullPath string) error {
return embedded.ExtractBytesToTarget(snykCLIBytes, targetFullPath)
}
2 changes: 1 addition & 1 deletion cliv2/internal/embedded/cliv1/dummy_embedded_legacy_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

var snykCLIBytes []byte = []byte("\n")

func getCLIv1Filename() string {
func GetCLIv1Filename() string {
return "FILENAME"
}

Expand Down
2 changes: 1 addition & 1 deletion cliv2/internal/embedded/cliv1/embedded_binary_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
//go:embed FILENAME
var snykCLIBytes []byte

func getCLIv1Filename() string {
func GetCLIv1Filename() string {
return "FILENAME"
}

Expand Down
3 changes: 2 additions & 1 deletion cliv2/pkg/basic_workflows/legacycli.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func legacycliWorkflow(
debugLogger := invocation.GetEnhancedLogger() // uses zerolog
debugLoggerDefault := invocation.GetLogger() // uses log
networkAccess := invocation.GetNetworkAccess()
ri := invocation.GetRuntimeInfo()

args := config.GetStringSlice(configuration.RAW_CMD_ARGS)
useStdIo := config.GetBool(configuration.WORKFLOW_USE_STDIO)
Expand All @@ -89,7 +90,7 @@ func legacycliWorkflow(

// init cli object
var cli *cliv2.CLI
cli, err = cliv2.NewCLIv2(config, debugLoggerDefault)
cli, err = cliv2.NewCLIv2(config, debugLoggerDefault, ri)
if err != nil {
return output, err
}
Expand Down
24 changes: 15 additions & 9 deletions test/jest/acceptance/extra-certs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Extra CA certificates specified with `NODE_EXTRA_CA_CERTS`', () => {
await server.listenWithHttps(port, { cert: certPem, key: keyPem });

// invoke WITHOUT additional certificate set => fails
const res1 = await runSnykCLI(`test --debug`, {
const res1Promise = runSnykCLI(`test --debug`, {
env: {
...process.env,
SNYK_API: SNYK_API,
Expand All @@ -65,7 +65,7 @@ describe('Extra CA certificates specified with `NODE_EXTRA_CA_CERTS`', () => {
});

// invoke WITH additional certificate set => succeeds
const res2 = await runSnykCLI(`test --debug`, {
const res2Promise = runSnykCLI(`test --debug`, {
env: {
...process.env,
NODE_EXTRA_CA_CERTS: 'cliv2/mytestcert.crt',
Expand All @@ -74,10 +74,8 @@ describe('Extra CA certificates specified with `NODE_EXTRA_CA_CERTS`', () => {
},
});

let res3 = { code: 2 };
let res4 = { code: 0 };
// invoke WITHOUT additional certificate set => succeeds
res3 = await runSnykCLI(
const res3Promise = runSnykCLI(
`sbom --debug --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.4+json`,
{
env: {
Expand All @@ -89,7 +87,7 @@ describe('Extra CA certificates specified with `NODE_EXTRA_CA_CERTS`', () => {
);

// invoke WITH additional certificate set => succeeds
res4 = await runSnykCLI(
const res4Promise = runSnykCLI(
`sbom --debug --org aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format cyclonedx1.4+json`,
{
env: {
Expand All @@ -101,14 +99,22 @@ describe('Extra CA certificates specified with `NODE_EXTRA_CA_CERTS`', () => {
},
);

const [res1, res2, res3, res4] = await Promise.all([
res1Promise,
res2Promise,
res3Promise,
res4Promise,
]);

await server.closePromise();

expect(res1.code).toBe(2);
expect(res2.code).toBe(0);
expect(res3.code).toBe(2);
expect(res4.code).toBe(0);
fs.unlink('cliv2/mytestcert.crt', () => {});
fs.unlink('cliv2/mytestcert.key', () => {});
fs.unlink('cliv2/mytestcert.pem', () => {});

fs.unlinkSync('cliv2/mytestcert.crt');
fs.unlinkSync('cliv2/mytestcert.key');
fs.unlinkSync('cliv2/mytestcert.pem');
});
});
9 changes: 5 additions & 4 deletions test/jest/acceptance/parallel-execution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ describe('Parallel CLI execution', () => {
singleTestResult.push(runSnykCLI(`test -d`, { cwd: project.path() }));
}

for (let i = 0; i < numberOfParallelExecutions; i++) {
const { code } = await singleTestResult[i];
expect(code).toBe(1);
}
const results = await Promise.all(singleTestResult);

results.forEach((result) => {
expect(result.code).toBe(1);
});
});
});
19 changes: 11 additions & 8 deletions test/jest/acceptance/woof.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ describe('woof', () => {
});

// test each supported language code
test.each(languages)('Woofs in %s', async ({ langCode, expectedWoof }) => {
const { stdout, code, stderr } = await runSnykCLI(
`woof --language=${langCode}`,
);
expect(stdout).toContain(expectedWoof);
expect(code).toBe(0);
expect(stderr).toBe('');
});
test.concurrent.each(languages)(
'Woofs in %s',
async ({ langCode, expectedWoof }) => {
const { stdout, code, stderr } = await runSnykCLI(
`woof --language=${langCode}`,
);
expect(stdout).toContain(expectedWoof);
expect(code).toBe(0);
expect(stderr).toBe('');
},
);
});

0 comments on commit 0a6a560

Please sign in to comment.