@@ -3,9 +3,12 @@ package cmd
33import (
44 "bytes"
55 "context"
6+ "crypto/sha256"
67 "errors"
8+ "fmt"
79 "net/http/httptest"
810 "os"
11+ "os/exec"
912 "path/filepath"
1013 "strings"
1114 "testing"
@@ -14,6 +17,7 @@ import (
1417
1518 "github.com/CircleCI-Public/chunk-cli/internal/circleci"
1619 "github.com/CircleCI-Public/chunk-cli/internal/config"
20+ "github.com/CircleCI-Public/chunk-cli/internal/session"
1721 "github.com/CircleCI-Public/chunk-cli/internal/testing/fakes"
1822)
1923
@@ -159,3 +163,134 @@ func TestValidateEnvFlagBadValue(t *testing.T) {
159163 assert .Assert (t , err != nil )
160164 assert .Assert (t , strings .Contains (err .Error (), "BADVALUE" ), "got: %v" , err )
161165}
166+
167+ // gitSetup initialises a minimal git repo at dir on the given branch name.
168+ func gitSetup (t * testing.T , dir , branch string ) {
169+ t .Helper ()
170+ run := func (args ... string ) {
171+ t .Helper ()
172+ c := exec .Command ("git" , append ([]string {"-C" , dir }, args ... )... )
173+ out , err := c .CombinedOutput ()
174+ if err != nil {
175+ t .Fatalf ("git %v: %v\n %s" , args , err , out )
176+ }
177+ }
178+ run ("init" , "-b" , branch )
179+ run ("config" , "user.email" , "test@example.com" )
180+ run ("config" , "user.name" , "Test" )
181+ _ = os .WriteFile (filepath .Join (dir , "README" ), []byte ("init" ), 0o644 )
182+ run ("add" , "." )
183+ run ("commit" , "-m" , "init" )
184+ }
185+
186+ func hashFor (sessionID , branch string ) string {
187+ sum := sha256 .Sum256 ([]byte (sessionID + ":" + branch ))
188+ return fmt .Sprintf ("%x" , sum [:4 ])
189+ }
190+
191+ // Tests with a session ID: branch must be hashed, never appear raw.
192+
193+ func TestSidecarAutoNameWithSessionAndBranch (t * testing.T ) {
194+ dir := t .TempDir ()
195+ gitSetup (t , dir , "main" )
196+ ctx := session .WithID (context .Background (), "sess-1" )
197+ got := sidecarAutoName (ctx , dir )
198+ want := filepath .Base (dir ) + "-sess-1-" + hashFor ("sess-1" , "main" )
199+ assert .Equal (t , got , want )
200+ }
201+
202+ func TestSidecarAutoNameWithSessionBranchWithSlashes (t * testing.T ) {
203+ dir := t .TempDir ()
204+ gitSetup (t , dir , "main" )
205+ run := func (args ... string ) {
206+ t .Helper ()
207+ c := exec .Command ("git" , append ([]string {"-C" , dir }, args ... )... )
208+ out , err := c .CombinedOutput ()
209+ if err != nil {
210+ t .Fatalf ("git %v: %v\n %s" , args , err , out )
211+ }
212+ }
213+ run ("checkout" , "-b" , "feature/my-branch" )
214+ ctx := session .WithID (context .Background (), "sess-2" )
215+ got := sidecarAutoName (ctx , dir )
216+ want := filepath .Base (dir ) + "-sess-2-" + hashFor ("sess-2" , "feature/my-branch" )
217+ assert .Equal (t , got , want )
218+ assert .Assert (t , ! strings .Contains (got , "feature" ), "raw branch must not appear in name, got %q" , got )
219+ assert .Assert (t , ! strings .Contains (got , "my-branch" ), "raw branch must not appear in name, got %q" , got )
220+ }
221+
222+ func TestSidecarAutoNameWithSessionNoBranch (t * testing.T ) {
223+ dir := t .TempDir ()
224+ // No git repo → no branch.
225+ ctx := session .WithID (context .Background (), "sess-3" )
226+ got := sidecarAutoName (ctx , dir )
227+ assert .Equal (t , got , filepath .Base (dir )+ "-sess-3" )
228+ }
229+
230+ func TestSidecarAutoNameDifferentBranchesDifferentNames (t * testing.T ) {
231+ dir := t .TempDir ()
232+ gitSetup (t , dir , "main" )
233+ run := func (args ... string ) {
234+ t .Helper ()
235+ c := exec .Command ("git" , append ([]string {"-C" , dir }, args ... )... )
236+ out , err := c .CombinedOutput ()
237+ if err != nil {
238+ t .Fatalf ("git %v: %v\n %s" , args , err , out )
239+ }
240+ }
241+ ctx := session .WithID (context .Background (), "sess-x" )
242+ n1 := sidecarAutoName (ctx , dir )
243+ run ("checkout" , "-b" , "other-branch" )
244+ n2 := sidecarAutoName (ctx , dir )
245+ assert .Assert (t , n1 != n2 , "different branches must produce different names: %q vs %q" , n1 , n2 )
246+ }
247+
248+ // Tests without a session ID: legacy sanitised-branch fallback.
249+
250+ func TestSidecarAutoNameNoSessionBranchPresent (t * testing.T ) {
251+ dir := t .TempDir ()
252+ gitSetup (t , dir , "main" )
253+ got := sidecarAutoName (context .Background (), dir )
254+ assert .Equal (t , got , filepath .Base (dir )+ "-main-validate" )
255+ }
256+
257+ func TestSidecarAutoNameNoSessionBranchAbsent (t * testing.T ) {
258+ dir := t .TempDir ()
259+ // No git repo → falls back to old format.
260+ got := sidecarAutoName (context .Background (), dir )
261+ assert .Equal (t , got , filepath .Base (dir )+ "-validate" )
262+ }
263+
264+ func TestSidecarAutoNameNoSessionBranchWithSlashes (t * testing.T ) {
265+ dir := t .TempDir ()
266+ gitSetup (t , dir , "main" )
267+ run := func (args ... string ) {
268+ t .Helper ()
269+ c := exec .Command ("git" , append ([]string {"-C" , dir }, args ... )... )
270+ out , err := c .CombinedOutput ()
271+ if err != nil {
272+ t .Fatalf ("git %v: %v\n %s" , args , err , out )
273+ }
274+ }
275+ run ("checkout" , "-b" , "feature/my-branch" )
276+ got := sidecarAutoName (context .Background (), dir )
277+ assert .Equal (t , got , filepath .Base (dir )+ "-feature-my-branch-validate" )
278+ }
279+
280+ func TestSidecarAutoNameNoSessionLongBranch (t * testing.T ) {
281+ dir := t .TempDir ()
282+ long := "abcdefghijklmnopqrstuvwxyz012345" // 32 chars
283+ gitSetup (t , dir , "main" )
284+ run := func (args ... string ) {
285+ t .Helper ()
286+ c := exec .Command ("git" , append ([]string {"-C" , dir }, args ... )... )
287+ out , err := c .CombinedOutput ()
288+ if err != nil {
289+ t .Fatalf ("git %v: %v\n %s" , args , err , out )
290+ }
291+ }
292+ run ("checkout" , "-b" , long )
293+ got := sidecarAutoName (context .Background (), dir )
294+ // branch truncated to 30 chars
295+ assert .Equal (t , got , filepath .Base (dir )+ "-" + long [:30 ]+ "-validate" )
296+ }
0 commit comments