2021-09-14 16:14:50 +02:00
package cnbutils
import (
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
func TestOrderSave ( t * testing . T ) {
2021-10-01 13:30:06 +02:00
t . Run ( "successfully Encode struct to toml format (multiple buildpacks)" , func ( t * testing . T ) {
2021-09-14 16:14:50 +02:00
mockUtils := MockUtils {
ExecMockRunner : & mock . ExecMockRunner { } ,
FilesMock : & mock . FilesMock { } ,
DockerMock : & DockerMock { } ,
}
2021-10-01 13:30:06 +02:00
testBuildpacks := [ ] BuildPackMetadata {
{
ID : "paketo-buildpacks/sap-machine" ,
Version : "1.1.1" ,
} ,
{
ID : "paketo-buildpacks/java" ,
Version : "2.2.2" ,
} ,
}
2021-09-14 16:14:50 +02:00
testOrder := Order {
Utils : mockUtils ,
}
2021-10-01 13:30:06 +02:00
var testEntry OrderEntry
testEntry . Group = append ( testEntry . Group , testBuildpacks ... )
testOrder . Order = [ ] OrderEntry { testEntry }
2021-09-14 16:14:50 +02:00
err := testOrder . Save ( "/tmp/order.toml" )
assert . NoError ( t , err )
assert . True ( t , mockUtils . HasWrittenFile ( "/tmp/order.toml" ) )
result , err := mockUtils . FileRead ( "/tmp/order.toml" )
assert . NoError ( t , err )
2021-10-01 13:30:06 +02:00
assert . Equal ( t , "\n[[order]]\n\n [[order.group]]\n id = \"paketo-buildpacks/sap-machine\"\n version = \"1.1.1\"\n\n [[order.group]]\n id = \"paketo-buildpacks/java\"\n version = \"2.2.2\"\n" , string ( result ) )
2021-09-14 16:14:50 +02:00
} )
t . Run ( "raises an error if unable to write the file" , func ( t * testing . T ) {
mockUtils := MockUtils {
ExecMockRunner : & mock . ExecMockRunner { } ,
FilesMock : & mock . FilesMock { } ,
DockerMock : & DockerMock { } ,
}
mockUtils . FileWriteErrors = map [ string ] error {
"/tmp/order.toml" : fmt . Errorf ( "unable to write to file" ) ,
}
testOrder := Order {
Utils : mockUtils ,
}
err := testOrder . Save ( "/tmp/order.toml" )
assert . Error ( t , err , "unable to write to file" )
assert . False ( t , mockUtils . HasWrittenFile ( "/tmp/order.toml" ) )
} )
}