@@ -5,34 +5,62 @@ package helpers
55
66import (
77 "os"
8- "os/exec"
98 "path/filepath"
109 "testing"
1110
1211 "github.com/stretchr/testify/assert"
1312 "github.com/stretchr/testify/require"
1413)
1514
16- func TestRepoRoot (t * testing.T ) {
17- t .Run ("returns_repo_root_from_subdirectory" , func (t * testing.T ) {
18- wd , err := os .Getwd ()
15+ func TestRootModuleDir (t * testing.T ) {
16+ t .Run ("finds_go_mod_in_parent" , func (t * testing.T ) {
17+ tmp := t .TempDir ()
18+ subDir := filepath .Join (tmp , "sub" )
19+ require .NoError (t , os .MkdirAll (subDir , 0o700 ))
20+
21+ require .NoError (t , os .WriteFile (filepath .Join (tmp , "go.mod" ), []byte ("module example.com/root\n " ), 0o600 ))
22+
23+ dir , err := rootModuleDir (subDir )
1924 require .NoError (t , err )
25+ assert .Equal (t , tmp , dir )
26+ })
27+
28+ t .Run ("finds_go_mod_in_intermediate_directory" , func (t * testing.T ) {
29+ tmp := t .TempDir ()
30+ projectDir := filepath .Join (tmp , "project" )
31+ componentDir := filepath .Join (projectDir , "receiver" , "foo" )
32+ require .NoError (t , os .MkdirAll (componentDir , 0o700 ))
2033
21- root , err := repoRoot (wd )
34+ // No go.mod at tmp root; go.mod is in project/ subdirectory
35+ require .NoError (t , os .WriteFile (filepath .Join (projectDir , "go.mod" ), []byte ("module example.com/project\n " ), 0o600 ))
36+
37+ dir , err := rootModuleDir (componentDir )
2238 require .NoError (t , err )
23- assert .DirExists (t , root )
24- assert .FileExists (t , filepath .Join (root , "go.mod" ))
39+ assert .Equal (t , projectDir , dir )
2540 })
2641
27- t .Run ("error_for_nonexistent_directory" , func (t * testing.T ) {
28- _ , err := repoRoot ("/nonexistent/path/that/does/not/exist" )
29- require .Error (t , err )
42+ t .Run ("prefers_highest_go_mod" , func (t * testing.T ) {
43+ tmp := t .TempDir ()
44+ componentDir := filepath .Join (tmp , "project" , "receiver" , "foo" )
45+ require .NoError (t , os .MkdirAll (componentDir , 0o700 ))
46+
47+ // go.mod at both levels
48+ require .NoError (t , os .WriteFile (filepath .Join (tmp , "go.mod" ), []byte ("module example.com/root\n " ), 0o600 ))
49+ require .NoError (t , os .WriteFile (filepath .Join (tmp , "project" , "go.mod" ), []byte ("module example.com/project\n " ), 0o600 ))
50+
51+ dir , err := rootModuleDir (componentDir )
52+ require .NoError (t , err )
53+ assert .Equal (t , tmp , dir )
3054 })
3155
32- t .Run ("error_outside_git_repo " , func (t * testing.T ) {
56+ t .Run ("error_when_no_go_mod_found " , func (t * testing.T ) {
3357 tmp := t .TempDir ()
34- _ , err := repoRoot (tmp )
58+ subDir := filepath .Join (tmp , "sub" )
59+ require .NoError (t , os .MkdirAll (subDir , 0o700 ))
60+
61+ _ , err := rootModuleDir (subDir )
3562 require .Error (t , err )
63+ assert .Contains (t , err .Error (), "no go.mod found" )
3664 })
3765}
3866
@@ -51,57 +79,37 @@ func TestRootPackage(t *testing.T) {
5179 require .Error (t , err )
5280 })
5381
54- t .Run ("error_when_go_mod_missing " , func (t * testing.T ) {
82+ t .Run ("resolves_module_from_synthetic_go_mod " , func (t * testing.T ) {
5583 tmp := t .TempDir ()
56- subDir := filepath .Join (tmp , "sub" )
57- require .NoError (t , os .MkdirAll (subDir , 0o700 ))
58-
59- gitInit (t , tmp )
60-
61- _ , err := RootPackage (subDir )
62- require .Error (t , err )
63- assert .Contains (t , err .Error (), "go.mod" )
64- })
65-
66- t .Run ("error_when_go_mod_has_no_module_directive" , func (t * testing.T ) {
67- tmp := t .TempDir ()
68- subDir := filepath .Join (tmp , "sub" )
84+ subDir := filepath .Join (tmp , "pkg" , "foo" )
6985 require .NoError (t , os .MkdirAll (subDir , 0o700 ))
7086
71- gitInit (t , tmp )
7287 require .NoError (t , os .WriteFile (
7388 filepath .Join (tmp , "go.mod" ),
74- []byte ("go 1.21\n " ),
89+ []byte ("module example.com/my-project \n \n go 1.21\n " ),
7590 0o600 ,
7691 ))
7792
78- _ , err := RootPackage (subDir )
79- require .Error (t , err )
80- assert .Contains (t , err . Error (), "module directive not found" )
93+ pkg , err := RootPackage (subDir )
94+ require .NoError (t , err )
95+ assert .Equal (t , "example.com/my-project" , pkg )
8196 })
8297
83- t .Run ("parses_module_from_synthetic_go_mod " , func (t * testing.T ) {
98+ t .Run ("monorepo_go_mod_not_at_top_level " , func (t * testing.T ) {
8499 tmp := t .TempDir ()
85- subDir := filepath .Join (tmp , "pkg" , "foo" )
86- require .NoError (t , os .MkdirAll (subDir , 0o700 ))
100+ projectDir := filepath .Join (tmp , "collector" )
101+ componentDir := filepath .Join (projectDir , "receiver" , "foo" )
102+ require .NoError (t , os .MkdirAll (componentDir , 0o700 ))
87103
88- gitInit ( t , tmp )
104+ // No go.mod at top level; only in collector/ subdirectory
89105 require .NoError (t , os .WriteFile (
90- filepath .Join (tmp , "go.mod" ),
91- []byte ("module example.com/my-project \n \n go 1.21\n " ),
106+ filepath .Join (projectDir , "go.mod" ),
107+ []byte ("module go.opentelemetry.io/collector \n \n go 1.21\n " ),
92108 0o600 ,
93109 ))
94110
95- pkg , err := RootPackage (subDir )
111+ pkg , err := RootPackage (componentDir )
96112 require .NoError (t , err )
97- assert .Equal (t , "example.com/my-project " , pkg )
113+ assert .Equal (t , "go.opentelemetry.io/collector " , pkg )
98114 })
99115}
100-
101- func gitInit (t * testing.T , dir string ) {
102- t .Helper ()
103- cmd := exec .Command ("git" , "init" )
104- cmd .Dir = dir
105- out , err := cmd .CombinedOutput ()
106- require .NoError (t , err , "git init failed: %s" , out )
107- }
0 commit comments