Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor file reading to use os package instead of ioutil
  • Loading branch information
dido18 committed Jun 10, 2025
commit 826b71b332c53d82ec669d42fdda16d834eb5c8f
6 changes: 3 additions & 3 deletions apt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ package apt
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestList(t *testing.T) {
out, err := ioutil.ReadFile("testdata/dpkg-query-output-1.txt")
out, err := os.ReadFile("testdata/dpkg-query-output-1.txt")
require.NoError(t, err, "Reading test input data")
list := parseDpkgQueryOutput(out)

// Check list with expected output
data, err := ioutil.ReadFile("testdata/dpkg-query-output-1-result.json")
data, err := os.ReadFile("testdata/dpkg-query-output-1-result.json")
require.NoError(t, err, "Reading test result data")
var expected []*Package
err = json.Unmarshal(data, &expected)
Expand Down
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/arduino/go-apt-client

go 1.24.0

require github.com/stretchr/testify v1.10.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 6 additions & 7 deletions repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -127,7 +126,7 @@ func parseAPTConfigLine(line string) *Repository {
}

func parseAPTConfigFile(configPath string) (RepositoryList, error) {
data, err := ioutil.ReadFile(configPath)
data, err := os.ReadFile(configPath)
if err != nil {
return nil, fmt.Errorf("Reading %s: %s", configPath, err)
}
Expand All @@ -153,7 +152,7 @@ func ParseAPTConfigFolder(folderPath string) (RepositoryList, error) {
sources := []string{filepath.Join(folderPath, "sources.list")}

sourcesFolder := filepath.Join(folderPath, "sources.list.d")
list, err := ioutil.ReadDir(sourcesFolder)
list, err := os.ReadDir(sourcesFolder)
if err != nil {
return nil, fmt.Errorf("Reading %s folder: %s", sourcesFolder, err)
}
Expand Down Expand Up @@ -219,7 +218,7 @@ func RemoveRepository(repo *Repository, configFolderPath string) error {

// Read the config file that contains the repo config to remove
fileToFilter := repoToRemove.configFile
data, err := ioutil.ReadFile(fileToFilter)
data, err := os.ReadFile(fileToFilter)
if err != nil {
return fmt.Errorf("Reading config file %s: %s", fileToFilter, err)
}
Expand All @@ -230,7 +229,7 @@ func RemoveRepository(repo *Repository, configFolderPath string) error {
for scanner.Scan() {
line := scanner.Text()
r := parseAPTConfigLine(line)
if r!= nil && r.Equals(repo) {
if r != nil && r.Equals(repo) {
// Filter repo configs that match the repo to be removed
continue
}
Expand Down Expand Up @@ -262,7 +261,7 @@ func EditRepository(old *Repository, new *Repository, configFolderPath string) e

// Read the config file that contains the repo configuration to edit
fileToEdit := repoToEdit.configFile
data, err := ioutil.ReadFile(fileToEdit)
data, err := os.ReadFile(fileToEdit)
if err != nil {
return fmt.Errorf("Reading config file %s: %s", fileToEdit, err)
}
Expand Down Expand Up @@ -294,7 +293,7 @@ func replaceFile(path string, newContent []byte) error {
backupPath := path + ".save"

// Create the new version of the file
err := ioutil.WriteFile(newPath, newContent, 0644)
err := os.WriteFile(newPath, newContent, 0644)
if err != nil {
return fmt.Errorf("Creating replacement file for %s: %s", newPath, err)
}
Expand Down
3 changes: 1 addition & 2 deletions repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package apt

import (
"encoding/json"
"io/ioutil"
"os"
"testing"

Expand All @@ -31,7 +30,7 @@ func TestParseAPTConfigFolder(t *testing.T) {
repos, err := ParseAPTConfigFolder("testdata/apt")
require.NoError(t, err, "running List command")

expectedData, err := ioutil.ReadFile("testdata/TestParseAPTConfigFolder.json")
expectedData, err := os.ReadFile("testdata/TestParseAPTConfigFolder.json")
require.NoError(t, err, "Reading test data")
expected := []*Repository{}
err = json.Unmarshal(expectedData, &expected)
Expand Down