1// Copyright (C) 2022 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package local 16 17import ( 18 "bytes" 19 "context" 20 "reflect" 21 "testing" 22 23 "tools/treble/build/report/app" 24) 25 26// Test cases for local GIT. 27type TestCmd struct { 28 err error 29 text string 30} 31type gitTestCli struct { 32 revParse *TestCmd 33 remoteUrl *TestCmd 34 tree *TestCmd 35 commit *TestCmd 36 diffBranch *TestCmd 37} 38 39func (g *gitTestCli) ProjectInfo(ctx context.Context, gitDir, workDir string) (*bytes.Buffer, error) { 40 return bytes.NewBufferString(g.revParse.text), g.revParse.err 41} 42func (g *gitTestCli) RemoteUrl(ctx context.Context, gitDir, workDir, remote string) (*bytes.Buffer, error) { 43 return bytes.NewBufferString(g.remoteUrl.text), g.remoteUrl.err 44} 45func (g *gitTestCli) Tree(ctx context.Context, gitDir, workDir, revision string) (*bytes.Buffer, error) { 46 return bytes.NewBufferString(g.tree.text), g.tree.err 47} 48func (g *gitTestCli) CommitInfo(ctx context.Context, gitDir, workDir, sha string) (*bytes.Buffer, error) { 49 return bytes.NewBufferString(g.commit.text), g.tree.err 50} 51func (g *gitTestCli) DiffBranches(ctx context.Context, gitDir, workDir, upstream, sha string) (*bytes.Buffer, error) { 52 return bytes.NewBufferString(g.diffBranch.text), g.tree.err 53} 54 55func Test_git(t *testing.T) { 56 57 type projectTest struct { 58 revCmd *TestCmd 59 remoteCmd *TestCmd 60 treeCmd *TestCmd 61 res *app.GitProject 62 } 63 64 type commitTest struct { 65 sha string 66 cmd *TestCmd 67 res *app.GitCommit 68 } 69 70 tests := []struct { 71 path string 72 gitDir string 73 remote string 74 revision string 75 getFiles bool 76 project projectTest 77 commit commitTest 78 }{ 79 { 80 path: "work/dir", 81 gitDir: "", 82 remote: "origin", 83 revision: "sha_revision", 84 getFiles: true, 85 project: projectTest{ 86 revCmd: &TestCmd{text: "/abs/path/to/work/dir\nsha_revision\n", err: nil}, 87 remoteCmd: &TestCmd{text: "http://url/workdir", err: nil}, 88 treeCmd: &TestCmd{text: "", err: nil}, 89 res: &app.GitProject{ 90 RepoDir: "work/dir", 91 WorkDir: "/abs/path/to/work/dir", 92 GitDir: ".git", 93 Remote: "origin", 94 RemoteUrl: "http://url/workdir", 95 Revision: "sha_revision", 96 Files: make(map[string]*app.GitTreeObj)}, 97 }, 98 // Test empty commit 99 commit: commitTest{ 100 sha: "commit_sha", 101 cmd: &TestCmd{text: "commit_sha", err: nil}, 102 res: &app.GitCommit{Sha: "commit_sha", Files: []app.GitCommitFile{}}, 103 }, 104 }, 105 { 106 path: "work/dir", 107 gitDir: "", 108 remote: "origin", 109 revision: "sha_revision", 110 getFiles: true, 111 project: projectTest{ 112 revCmd: &TestCmd{text: "/abs/path/to/work/dir\nsha_revision\n", err: nil}, 113 remoteCmd: &TestCmd{text: "http://url/workdir", err: nil}, 114 treeCmd: &TestCmd{text: "100644 blob 0000000000000000000000000000000000000001 file.1\n", err: nil}, 115 res: &app.GitProject{ 116 RepoDir: "work/dir", 117 WorkDir: "/abs/path/to/work/dir", 118 GitDir: ".git", 119 Remote: "origin", 120 RemoteUrl: "http://url/workdir", 121 Revision: "sha_revision", 122 Files: map[string]*app.GitTreeObj{"file.1": &app.GitTreeObj{Permissions: "100644", Type: "blob", 123 Sha: "0000000000000000000000000000000000000001", Filename: "file.1"}}}, 124 }, 125 commit: commitTest{ 126 sha: "HEAD", 127 cmd: &TestCmd{text: "sha_for_head\nR removed.1\nA added.1\nM modified.1\n", err: nil}, 128 res: &app.GitCommit{ 129 Sha: "sha_for_head", 130 Files: []app.GitCommitFile{ 131 {Filename: "removed.1", Type: app.GitFileRemoved}, 132 {Filename: "added.1", Type: app.GitFileAdded}, 133 {Filename: "modified.1", Type: app.GitFileModified}, 134 }, 135 }, 136 }, 137 }, 138 } 139 for _, test := range tests { 140 git := &gitCli{git: &gitTestCli{ 141 revParse: test.project.revCmd, 142 remoteUrl: test.project.remoteCmd, 143 tree: test.project.treeCmd, 144 commit: test.commit.cmd, 145 }} 146 147 proj, err := git.Project(nil, test.path, test.gitDir, test.remote, test.revision) 148 if err != nil { 149 t.Fatal("Failed to parse project") 150 } 151 if test.getFiles { 152 _ = git.PopulateFiles(nil, proj, "") 153 } 154 if !reflect.DeepEqual(*proj, *test.project.res) { 155 t.Errorf("Project = %+v; want %+v", *proj, *test.project.res) 156 } 157 if test.commit.cmd != nil { 158 c, err := git.CommitInfo(nil, proj, test.commit.sha) 159 if err != nil { 160 t.Errorf("Failed to get; %v", test) 161 } else { 162 if !reflect.DeepEqual(*c, *test.commit.res) { 163 t.Errorf("Commit = %v; want %v", c, *test.commit.res) 164 } 165 } 166 } 167 } 168 169} 170