Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
attiasas committed Jan 9, 2025
1 parent 26b425d commit 2feff2d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 51 deletions.
25 changes: 24 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,31 @@ jobs:

# Test
- name: Run tests
run: go test ${{ env.GO_COMMON_TEST_ARGS }} --test.curation --test.enrich --test.git
run: go test ${{ env.GO_COMMON_TEST_ARGS }} --test.curation --test.enrich


Git_Commands_Integration_Tests:
name: "[${{ matrix.os }}] Git Commands Integration Tests"
needs: Pretest
runs-on: ${{ matrix.os }}-latest
strategy:
fail-fast: false
matrix:
os: [ ubuntu, windows, macos ]
steps:
# Prepare the environment
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Install and Setup Dependencies
uses: ./.github/actions/install-and-setup

# Test
- name: Run tests
run: go test ${{ env.GO_COMMON_TEST_ARGS }} --test.git

Code_Coverage:
name: Generate Code Coverage Report
if: github.event_name == 'pull_request_target'
Expand Down
6 changes: 0 additions & 6 deletions audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,6 @@ type auditCommandTestParams struct {
WithLicense bool
}

// run audit command with different flags and params for integration tests
func testAuditCommand(t *testing.T, testCli *coreTests.JfrogCli, params auditCommandTestParams) (string, error) {
args := append([]string{"audit"}, getAuditCmdArgs(params)...)
return testCli.RunCliCmdWithOutputs(t, args...)
}

func getAuditCmdArgs(params auditCommandTestParams) (args []string) {
if len(params.WorkingDirsToScan) > 0 {
args = append(args, "--working-dirs="+strings.Join(params.WorkingDirsToScan, ","))
Expand Down
21 changes: 6 additions & 15 deletions commands/git/audit/gitaudit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
ioUtils "github.com/jfrog/jfrog-client-go/utils/io"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/jfrog/jfrog-client-go/xsc/services"

sourceAudit "github.com/jfrog/jfrog-cli-security/commands/audit"
Expand Down Expand Up @@ -41,9 +40,9 @@ func (gaCmd *GitAuditCommand) ServerDetails() (*config.ServerDetails, error) {

func (gaCmd *GitAuditCommand) Run() (err error) {
// Detect git info
_, gitInfo, err := DetectGitInfo()
gitInfo, err := DetectGitInfo()
if err != nil {
return
return fmt.Errorf("failed to get git context: %v", err)
}
if gitInfo == nil {
// No Error but no git info = project is dirty
Expand All @@ -61,20 +60,12 @@ func (gaCmd *GitAuditCommand) Run() (err error) {
return sourceAudit.ProcessResultsAndOutput(auditResults, gaCmd.getResultWriter(auditResults), gaCmd.failBuild)
}

func DetectGitInfo() (gitManager *gitutils.GitManager, gitInfo *services.XscGitInfoContext, err error) {
gitManager, err = gitutils.NewGitManager(".")
if err != nil {
return nil, nil, fmt.Errorf("failed to found local git repository at the current directory: %v", err)
}
gitInfo, err = gitManager.GetGitContext()
func DetectGitInfo() (gitInfo *services.XscGitInfoContext, err error) {
gitManager, err := gitutils.NewGitManager(".")
if err != nil {
return nil, nil, fmt.Errorf("failed to get git context: %v", err)
}
if gitInfo == nil {
log.Warn("Failed to get git context: defaulting to audit command.")
return nil, nil, nil
return
}
return
return gitManager.GetGitContext()
}

func toAuditParams(params GitAuditParams) *sourceAudit.AuditParams {
Expand Down
Binary file added tests/testdata/git/projects/forked/forked.zip
Binary file not shown.
14 changes: 7 additions & 7 deletions utils/gitutils/gitmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ func (gm *GitManager) GetGitContext() (gitInfo *services.XscGitInfoContext, err
return nil, err
}
if !isClean {
log.Warn("Uncommitted changes found in the repository, not supported in git audit.")
return nil, nil
return nil, fmt.Errorf("uncommitted changes found in the repository, not supported in git audit")
}
log.Debug(fmt.Sprintf("Git Context: %+v", gitInfo))
return gitInfo, nil
Expand All @@ -137,11 +136,12 @@ func getRemoteUrl(remote *goGit.Remote) (remoteUrl string, err error) {
return remote.Config().URLs[0], nil
}

// Normalize the URL by removing protocol prefix and any trailing ".git"
func normalizeGitUrl(url string) string {
// Normalize the URL by removing "http://", "https://", and any trailing ".git"
url = strings.TrimSuffix(strings.TrimPrefix(url, "http://"), ".git")
url = strings.TrimSuffix(strings.TrimPrefix(url, "https://"), ".git")
return url
url = strings.TrimPrefix(url, "http://")
url = strings.TrimPrefix(url, "https://")
url = strings.TrimPrefix(url, "ssh://")
return strings.TrimSuffix(url, ".git")
}

func getGitRepoName(url string) string {
Expand All @@ -151,7 +151,7 @@ func getGitRepoName(url string) string {

func getGitProject(url string) string {
// First part after base Url is the owner/organization name.
urlParts := strings.Split(url, "/")
urlParts := strings.Split(normalizeGitUrl(url), "/")
if len(urlParts) < 2 {
log.Debug(fmt.Sprintf("Failed to get project name from URL: %s", url))
return ""
Expand Down
68 changes: 46 additions & 22 deletions utils/gitutils/gitmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ func TestGetGitContext(t *testing.T) {
testCases := []struct {
name string
testProjectZipDirPath string
NoDotGitFolder bool
gitInfo *services.XscGitInfoContext
}{
{
name: "No Git Info",
NoDotGitFolder: true,
testProjectZipDirPath: filepath.Join(basePath, "nogit"),
},
{
Expand All @@ -41,29 +43,14 @@ func TestGetGitContext(t *testing.T) {
BranchName: "main",
},
},
{
name: "Dirty Project (with uncommitted changes)",
testProjectZipDirPath: filepath.Join(basePath, "dirty"),
// gitInfo: &services.XscGitInfoContext{
// GitRepoHttpsCloneUrl: "https://github.com/attiasas/test-security-git.git",
// GitRepoName: "test-security-git",
// GitProject: "attiasas",
// GitProvider: "github",
// LastCommitUrl: "5fc36ff0666e5ce9dba6c0a1c539ee640cabe0b0",
// LastCommitMessage: "remove json",
// LastCommitAuthor: "attiasas",

// BranchName: "dirty_branch",
// },
},
{
name: "Self-Hosted Git Project (and SSO credentials)",
testProjectZipDirPath: filepath.Join(basePath, "selfhosted"),
gitInfo: &services.XscGitInfoContext{
GitRepoHttpsCloneUrl: "ssh://[email protected]/~assafa/test-security-git.git",
GitRepoName: "test-security-git",
// TODO: maybe detect provider as bb if ~ in the project name
GitProject: "assafa",
GitProject: "~assafa",
BranchName: "main",
LastCommitHash: "6abd0162f4e02e358124f74e89b30d1b1ff906bc",
LastCommitMessage: "initial commit",
Expand All @@ -79,14 +66,42 @@ func TestGetGitContext(t *testing.T) {
GitProject: "attiasas",
GitProvider: "gitlab",
BranchName: "main",
LastCommitHash: "5fc36ff0666e5ce9dba6c0a1c539ee640cabe0b0",
LastCommitHash: "ada14e9f525d8cbfb3c8c31ebe345d85ec342480",
LastCommitMessage: "add npm",
LastCommitAuthor: "attiasas",
},
},
// {
// name: "Forked Project (multiple remotes)",
// },
// Not supported yet
{
name: "Dirty Project (with uncommitted changes)",
testProjectZipDirPath: filepath.Join(basePath, "dirty"),
// gitInfo: &services.XscGitInfoContext{
// GitRepoHttpsCloneUrl: "https://github.com/attiasas/test-security-git.git",
// GitRepoName: "test-security-git",
// GitProject: "attiasas",
// GitProvider: "github",
// LastCommitUrl: "5fc36ff0666e5ce9dba6c0a1c539ee640cabe0b0",
// LastCommitMessage: "remove json",
// LastCommitAuthor: "attiasas",

// BranchName: "dirty_branch",
// },
},
{
name: "Forked Project (multiple remotes)",
testProjectZipDirPath: filepath.Join(basePath, "forked"),
// gitInfo: &services.XscGitInfoContext{
// GitRepoHttpsCloneUrl: "https://github.com/attiasas/test-security-git.git",
// GitRepoName: "test-security-git",
// GitProject: "attiasas",
// GitProvider: "github",
// LastCommitHash: "5fc36ff0666e5ce9dba6c0a1c539ee640cabe0b0",
// LastCommitMessage: "remove json",
// LastCommitAuthor: "attiasas",

// BranchName: "main",
// },
},
}

for _, testCase := range testCases {
Expand All @@ -96,16 +111,25 @@ func TestGetGitContext(t *testing.T) {
defer cleanUp()
// Run the test
gitManager, err := NewGitManager(".")
if testCase.gitInfo == nil {
if testCase.NoDotGitFolder {
// Assert no git info
assert.Error(t, err, goGit.ErrRepositoryNotExists.Error())
assert.Nil(t, gitManager)
return
} else if err != nil {
// Assert multiple remotes error
assert.Error(t, err, "multiple (2) remotes found, currently only one remote is supported, remotes: origin, upstream")
return
}
assert.NoError(t, err)
assert.NotNil(t, gitManager)
gitInfo, err := gitManager.GetGitContext()

if testCase.gitInfo == nil {
// Dirty project, we can't assert the git info
assert.Error(t, err)
assert.Nil(t, gitInfo)
return
}
// Assert the expected git info
require.NoError(t, err)
assert.Equal(t, testCase.gitInfo, gitInfo)
Expand Down

0 comments on commit 2feff2d

Please sign in to comment.