@@ -18,6 +18,11 @@ func TestAddJobLevelPermissions(t *testing.T) {
1818 }
1919
2020 for _ , f := range files {
21+
22+ if f .Name () == "empty-top-level-permissions.yml" {
23+ continue
24+ }
25+
2126 input , err := ioutil .ReadFile (path .Join (inputDirectory , f .Name ()))
2227
2328 if err != nil {
@@ -26,7 +31,7 @@ func TestAddJobLevelPermissions(t *testing.T) {
2631
2732 os .Setenv ("KBFolder" , "../../../knowledge-base/actions" )
2833
29- fixWorkflowPermsResponse , err := AddJobLevelPermissions (string (input ))
34+ fixWorkflowPermsResponse , err := AddJobLevelPermissions (string (input ), false )
3035 output := fixWorkflowPermsResponse .FinalOutput
3136 jobErrors := fixWorkflowPermsResponse .JobErrors
3237
@@ -68,6 +73,47 @@ func TestAddJobLevelPermissions(t *testing.T) {
6873 }
6974}
7075
76+ func TestAddJobLevelPermissionsWithEmptyTopLevel (t * testing.T ) {
77+ const inputDirectory = "../../../testfiles/joblevelpermskb/input"
78+ const outputDirectory = "../../../testfiles/joblevelpermskb/output"
79+
80+ // Test the empty-top-level-permissions.yml file
81+ input , err := ioutil .ReadFile (path .Join (inputDirectory , "empty-top-level-permissions.yml" ))
82+ if err != nil {
83+ t .Fatal (err )
84+ }
85+
86+ expectedOutput , err := ioutil .ReadFile (path .Join (outputDirectory , "empty-top-level-permissions.yml" ))
87+ if err != nil {
88+ t .Fatal (err )
89+ }
90+
91+ os .Setenv ("KBFolder" , "../../../knowledge-base/actions" )
92+
93+ // Test with addEmptyTopLevelPermissions = true
94+ fixWorkflowPermsResponse , err := AddJobLevelPermissions (string (input ), true )
95+ if err != nil {
96+ t .Errorf ("Unexpected error with addEmptyTopLevelPermissions=true: %v" , err )
97+ }
98+
99+ if fixWorkflowPermsResponse .FinalOutput != string (expectedOutput ) {
100+ t .Errorf ("test failed with addEmptyTopLevelPermissions=true for empty-top-level-permissions.yml\n Expected:\n %s\n \n Got:\n %s" ,
101+ string (expectedOutput ), fixWorkflowPermsResponse .FinalOutput )
102+ }
103+
104+ // Test with addEmptyTopLevelPermissions = false (should skip contents: read)
105+ fixWorkflowPermsResponse2 , err2 := AddJobLevelPermissions (string (input ), false )
106+ if err2 != nil {
107+ t .Errorf ("Unexpected error with addEmptyTopLevelPermissions=false: %v" , err2 )
108+ }
109+
110+ // With false, contents: read should be skipped at job level
111+ if fixWorkflowPermsResponse2 .FinalOutput != string (input ) {
112+ t .Errorf ("test failed with addEmptyTopLevelPermissions=false for empty-top-level-permissions.yml\n Expected:\n %s\n \n Got:\n %s" ,
113+ string (input ), fixWorkflowPermsResponse2 .FinalOutput )
114+ }
115+ }
116+
71117func Test_addPermissions (t * testing.T ) {
72118 type args struct {
73119 inputYaml string
@@ -112,6 +158,10 @@ func TestAddWorkflowLevelPermissions(t *testing.T) {
112158 continue
113159 }
114160
161+ if f .Name () == "empty-permissions.yml" {
162+ continue
163+ }
164+
115165 input , err := ioutil .ReadFile (path .Join (inputDirectory , f .Name ()))
116166
117167 if err != nil {
@@ -125,7 +175,7 @@ func TestAddWorkflowLevelPermissions(t *testing.T) {
125175 addProjectComment = true
126176 }
127177
128- output , err := AddWorkflowLevelPermissions (string (input ), addProjectComment )
178+ output , err := AddWorkflowLevelPermissions (string (input ), addProjectComment , false )
129179
130180 if err != nil {
131181 t .Errorf ("Error not expected" )
@@ -143,3 +193,41 @@ func TestAddWorkflowLevelPermissions(t *testing.T) {
143193 }
144194
145195}
196+
197+ func TestAddWorkflowLevelPermissionsWithEmpty (t * testing.T ) {
198+ const inputDirectory = "../../../testfiles/toplevelperms/input"
199+ const outputDirectory = "../../../testfiles/toplevelperms/output"
200+
201+ // Test the empty-permissions.yml file
202+ input , err := ioutil .ReadFile (path .Join (inputDirectory , "empty-permissions.yml" ))
203+ if err != nil {
204+ t .Fatal (err )
205+ }
206+
207+ expectedOutput , err := ioutil .ReadFile (path .Join (outputDirectory , "empty-permissions.yml" ))
208+ if err != nil {
209+ t .Fatal (err )
210+ }
211+
212+ // Test with addEmptyTopLevelPermissions = true
213+ output , err := AddWorkflowLevelPermissions (string (input ), false , true )
214+ if err != nil {
215+ t .Errorf ("Unexpected error with addEmptyTopLevelPermissions=true: %v" , err )
216+ }
217+
218+ if output != string (expectedOutput ) {
219+ t .Errorf ("test failed with addEmptyTopLevelPermissions=true for empty-permissions.yml\n Expected:\n %s\n \n Got:\n %s" ,
220+ string (expectedOutput ), output )
221+ }
222+
223+ // Test with addEmptyTopLevelPermissions = false (should add contents: read)
224+ output2 , err2 := AddWorkflowLevelPermissions (string (input ), false , false )
225+ if err2 != nil {
226+ t .Errorf ("Unexpected error with addEmptyTopLevelPermissions=false: %v" , err2 )
227+ }
228+
229+ // With false, should add contents: read instead of empty permissions
230+ if ! strings .Contains (output2 , "contents: read" ) || strings .Contains (output2 , "permissions: {}" ) {
231+ t .Errorf ("test failed with addEmptyTopLevelPermissions=false for empty-permissions.yml - should contain 'contents: read' but not 'permissions: {}'\n Got:\n %s" , output2 )
232+ }
233+ }
0 commit comments