Skip to content

Commit

Permalink
feat: Adding Savetofile() and its test
Browse files Browse the repository at this point in the history
  • Loading branch information
RawanMostafa08 committed Sep 10, 2024
1 parent 8d41227 commit a13f8f4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
33 changes: 30 additions & 3 deletions pkg/iniparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"reflect"
"sort"
"strings"
)

Expand Down Expand Up @@ -103,11 +104,37 @@ func (i *iniParser) Set(sectionName string, key string, value string) error {

func (i *iniParser) ToString() string {
var result string
for sectionName, section := range i.sections {
sectionNames := make([]string, 0)
for sectionName := range i.sections {
sectionNames = append(sectionNames, sectionName)
}
sort.Strings(sectionNames)

for _, sectionName := range sectionNames {
keys := make([]string, 0)
result += "[" + sectionName + "]\n"
for key, value := range section.map_ {
result += key + "=" + value + "\n"
for key := range i.sections[sectionName].map_ {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
result += key + " = " + i.sections[sectionName].map_[key] + "\n"
}
}
return result
}

func (i *iniParser) SaveToFile(path string) error {
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("error in opening the file: %v", err)
}
defer file.Close()

stringFile := i.ToString()
_, err = file.WriteString(stringFile)
if err != nil {
return fmt.Errorf("error in writing to the file: %v", err)
}
return nil
}
35 changes: 33 additions & 2 deletions pkg/iniparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package iniparser

import (
"fmt"
"os"
"reflect"
"testing"
)
Expand All @@ -16,7 +17,10 @@ organization = Acme Widgets Inc.
[database]
; use IP address in case network name resolution is not working
server = 192.0.2.62
port = 143`
port = 143
[section]
key0 = val0
key1 = val1`

func populateExpectedNormal(t *testing.T) map[string]section {
t.Helper()
Expand All @@ -33,6 +37,12 @@ func populateExpectedNormal(t *testing.T) map[string]section {
"port": "143",
},
},
"section": {
map_: map[string]string{
"key0": "val0",
"key1": "val1",
},
},
}
return expected
}
Expand Down Expand Up @@ -63,6 +73,16 @@ func assertEquality(t *testing.T, obj1 any, obj2 any) {
}
}

func assertFile(t *testing.T, filePath string, expectedData string) {
t.Helper()
data, err := os.ReadFile(filePath)
if err != nil {
t.Errorf("Error! : %v\n", err)
}
assertEquality(t, expectedData, string(data))

}

func TestLoadFromString(t *testing.T) {
t.Run("test normal ini file", func(t *testing.T) {

Expand Down Expand Up @@ -106,7 +126,7 @@ func TestGetSectionNames(t *testing.T) {
parser.LoadFromFile(path)
names := parser.GetSectionNames()

expected := []string{"owner", "database"}
expected := []string{"owner", "database", "section"}

assertEquality(t, expected, names)
}
Expand Down Expand Up @@ -222,3 +242,14 @@ func TestToString(t *testing.T) {

assertEquality(t, parser1.sections, parser2.sections)
}

func TestSaveToFile(t *testing.T) {
const outPath = "testdata/out.ini"
parser := InitParser()
parser.LoadFromFile(path)

parser.SaveToFile(outPath)

Check failure on line 251 in pkg/iniparser_test.go

View workflow job for this annotation

GitHub Actions / golangci

Error return value of `parser.SaveToFile` is not checked (errcheck)

stringFile := parser.ToString()
assertFile(t, outPath, stringFile)
}
3 changes: 3 additions & 0 deletions pkg/testdata/file.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ organization = Acme Widgets Inc.
[database]
server = 192.0.2.62
port = 143
[section]
key0 = val0
key1 = val1
9 changes: 9 additions & 0 deletions pkg/testdata/out.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[database]
port = 143
server = 192.0.2.62
[owner]
name = John Doe
organization = Acme Widgets Inc.
[section]
key0 = val0
key1 = val1

0 comments on commit a13f8f4

Please sign in to comment.