Skip to content

Commit

Permalink
Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalb4doc committed Dec 20, 2024
1 parent 26fe468 commit 3fc3c0e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions utils/usage/visibility_system_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package usage_test

import (
"testing"

"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/testsutils"
"github.com/stretchr/testify/assert"
)

func TestCreateMetric(t *testing.T) {
// Set environment variables for the test using SetEnvWithCallbackAndAssert
envVars := map[string]string{
"JFROG_CLI_USAGE_OIDC_USED": "true",
"JFROG_CLI_USAGE_JOB_ID": "job123",
"JFROG_CLI_USAGE_RUN_ID": "run456",
"JFROG_CLI_USAGE_GIT_REPO": "test-repo",
"JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED": "true",
}
cleanupFuncs := []func(){}
for key, value := range envVars {
cleanup := testsutils.SetEnvWithCallbackAndAssert(t, key, value)
cleanupFuncs = append(cleanupFuncs, cleanup)
}
defer func() {
for _, cleanup := range cleanupFuncs {
cleanup()
}
}()

commandName := "testCommand"
metric, err := createMetric(commandName)

Check failure on line 32 in utils/usage/visibility_system_manager_test.go

View workflow job for this annotation

GitHub Actions / Static-Check

undefined: createMetric (typecheck)
assert.NoError(t, err)

// Define the expected JSON structure
expectedJSON := `{
"value": 1,
"metrics_name": "jfcli_commands_countaa",
"labels": {
"product_id": "` + coreutils.GetCliUserAgentName() + `",
"feature_id": "testCommand",
"oidc_used": "true",
"job_id": "job123",
"run_id": "run456",
"git_repo": "https://github.com/jfrog/test-repo",
"gh_token_for_code_scanning_alerts_provided": "true"
}
}`

// Compare the generated JSON to the expected JSON
assert.JSONEq(t, expectedJSON, string(metric))
}

0 comments on commit 3fc3c0e

Please sign in to comment.