Merge pull request #6 from kairos-io/dont-let-mapping-be-nil

Don't let the mapping be nil when reading an empty file
This commit is contained in:
Dimitris Karakasilis 2022-11-16 09:30:31 +02:00 committed by GitHub
commit 7d077c9353
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -71,6 +71,10 @@ func (pi PartitionInfo) UpdateMapping(partitionData string) error {
return pi.save()
}
func (pi PartitionInfo) IsMappingNil() bool {
return pi.mapping == nil
}
func (pi PartitionInfo) save() error {
data, err := yaml.Marshal(&pi.mapping)
if err != nil {
@ -107,6 +111,9 @@ func ParsePartitionInfoFile(file string) (map[string]string, error) {
if err != nil {
return result, errors.Wrap(err, "unmarshalling partition info file")
}
if result == nil {
result = map[string]string{}
}
return result, nil
}

View File

@ -38,13 +38,15 @@ var _ = Describe("Partition Info file parsing", func() {
fmt.Sprintf("partition-info-%d.yaml", time.Now().UnixNano()))
})
It("creates the file and returns 'false' and an (empty) mapping", func() {
It("creates the file and returns 'false' and a non nil mapping", func() {
result, existed, err := pi.NewPartitionInfoFromFile(fileName)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(existed).To(BeFalse())
_, err = os.Stat(fileName)
Expect(err).ToNot(HaveOccurred())
Expect(result).ToNot(BeNil())
Expect(result.IsMappingNil()).To(BeFalse())
})
})
})