Skip to content

Commit

Permalink
add: unit tests for some of the new functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreznel committed Aug 16, 2019
1 parent 70f28fd commit 6ce29a8
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 41 deletions.
43 changes: 2 additions & 41 deletions cmd/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
"github.com/dpastoor/goutils"
"github.com/metrumresearchgroup/pkgr/testhelper"

"github.com/metrumresearchgroup/pkgr/desc"
"github.com/metrumresearchgroup/pkgr/pacman"
Expand Down Expand Up @@ -36,7 +36,7 @@ func InitializeTestEnvironment(fileSystem afero.Fs, goldenSet, testName string)
testWorkDir := filepath.Join("testsite", "working", testName)
fileSystem.MkdirAll(testWorkDir, 0755)

err := CopyDir(fileSystem, goldenSetPath, testWorkDir)
err := testhelper.CopyDir(fileSystem, goldenSetPath, testWorkDir)

if err != nil {
panic(err)
Expand Down Expand Up @@ -88,43 +88,4 @@ func (suite *PlanTestSuite) TestGetPriorInstalledPackages_NoPreinstalledPackages

func installedPackagesAreEqual(expected, actual desc.Desc) bool {
return expected.Package == actual.Package && expected.Version == actual.Version && expected.Repository == actual.Repository
}

func CopyDir(fs afero.Fs, src string, dst string) error {

err := fs.MkdirAll(dst, 0755)
if err != nil {
return err
}

openedDir, err := fs.Open(src)
if err != nil {
return err
}

directoryContents, err := openedDir.Readdir(0)
openedDir.Close()
if err != nil {
return err
}

for _, item := range directoryContents {
srcSubPath := filepath.Join(src, item.Name())
dstSubPath := filepath.Join(dst, item.Name())
if item.IsDir() {
fs.Mkdir(dstSubPath, item.Mode())
err := CopyDir(fs, srcSubPath, dstSubPath)
if err != nil {
return err
}
} else {
_, err := goutils.CopyFS(fs, srcSubPath, dstSubPath)
if err != nil {
fmt.Print("Received error: ")
fmt.Println(err)
return err
}
}
}
return nil
}
118 changes: 118 additions & 0 deletions rollback/operations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package rollback

import (
"fmt"
"github.com/metrumresearchgroup/pkgr/testhelper"
"github.com/spf13/afero"
"github.com/stretchr/testify/suite"
"path/filepath"
"testing"
)

type OperationsTestSuite struct {
suite.Suite
FileSystem afero.Fs
FilePrefix string
}

func (suite *OperationsTestSuite) SetupTest() {
suite.FileSystem = afero.NewOsFs()
suite.FilePrefix = "testsite/working"
}

func (suite *OperationsTestSuite) TearDownTest() {
mm := afero.NewOsFs()
mm.RemoveAll("testsite/working")
}

func TestOperationsTestSuite(t *testing.T) {
suite.Run(t, new(OperationsTestSuite))
}

func InitializeTestEnvironment(fileSystem afero.Fs, goldenSet string) {
goldenSetPath := filepath.Join("testsite", "golden", goldenSet)
testWorkDir := filepath.Join("testsite", "working")
fileSystem.MkdirAll(testWorkDir, 0755)

err := testhelper.CopyDir(fileSystem, goldenSetPath, testWorkDir)

if err != nil {
panic(err)
}
}

func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_DeletesOnlyNewPackages() {
InitializeTestEnvironment(suite.FileSystem, "basic-test1")

cwd, _ := filepath.Abs(".")
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd))

libraryPath, _ := filepath.Abs(filepath.Join("testsite", "working", "test-library"))

rbpFixture := RollbackPlan{
NewPackages: []string {"R6"},
Library: libraryPath,
}

RollbackPackageEnvironment(suite.FileSystem, rbpFixture)

suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6")))
suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon")))
}

func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_DeletesMultiplePackages() {
InitializeTestEnvironment(suite.FileSystem, "basic-test1")

cwd, _ := filepath.Abs(".")
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd))

libraryPath, _ := filepath.Abs(filepath.Join("testsite", "working", "test-library"))

rbpFixture := RollbackPlan{
NewPackages: []string {"R6", "crayon"},
Library: libraryPath,
}

RollbackPackageEnvironment(suite.FileSystem, rbpFixture)

suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6")))
suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon")))
}

func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_HandlesEmptyListOfPackages() {
InitializeTestEnvironment(suite.FileSystem, "basic-test1")

cwd, _ := filepath.Abs(".")
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd))

libraryPath, _ := filepath.Abs(filepath.Join(suite.FilePrefix, "basic-test1", "test-library"))

rbpFixture := RollbackPlan{
NewPackages: []string {},
Library: libraryPath,
}

RollbackPackageEnvironment(suite.FileSystem, rbpFixture )

suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6")))
suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon")))
}

func (suite *OperationsTestSuite) TestRollbackPackageEnvironment_PackagesAreCaseSensitive() {
InitializeTestEnvironment(suite.FileSystem, "basic-test1")

cwd, _ := filepath.Abs(".")
fmt.Println(fmt.Sprintf("Starting test with working directory %s", cwd))

libraryPath, _ := filepath.Abs(filepath.Join("testsite", "working", "test-library"))

rbpFixture := RollbackPlan{
NewPackages: []string {"R6", "CRAYON"},
Library: libraryPath,
}

RollbackPackageEnvironment(suite.FileSystem, rbpFixture)

suite.False(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "R6")))
suite.True(afero.DirExists(suite.FileSystem, filepath.Join(suite.FilePrefix, "test-library", "crayon")))
}
11 changes: 11 additions & 0 deletions rollback/testsite/golden/basic-test1/pkgr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Version: 1
# top level packages
Packages:
- R6
- crayon

# any repositories, order matters
Repos:
- CRAN: "https://cran.rstudio.com"

Library: "test-library"
24 changes: 24 additions & 0 deletions rollback/testsite/golden/basic-test1/test-library/R6/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Package: R6
Title: Encapsulated Classes with Reference Semantics
Version: 2.4.0
Authors@R: person("Winston", "Chang", role = c("aut", "cre"), email = "winston@stdout.org")
Description: Creates classes with reference semantics, similar to R's built-in
reference classes. Compared to reference classes, R6 classes are simpler
and lighter-weight, and they are not built on S4 classes so they do not
require the methods package. These classes allow public and private
members, and they support inheritance, even when the classes are defined in
different packages.
Depends: R (>= 3.0)
Suggests: knitr, microbenchmark, pryr, testthat, ggplot2, scales
License: MIT + file LICENSE
URL: https://r6.r-lib.org, https://github.com/r-lib/R6/
LazyData: true
BugReports: https://github.com/r-lib/R6/issues
RoxygenNote: 6.1.1
NeedsCompilation: no
Packaged: 2019-02-14 18:34:54 UTC; winston
Author: Winston Chang [aut, cre]
Maintainer: Winston Chang <winston@stdout.org>
Repository: CRAN
Date/Publication: 2019-02-14 19:42:14 UTC
Built: R 3.5.2; ; 2019-02-15 15:07:41 UTC; unix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Package: crayon
Title: Colored Terminal Output
Version: 1.3.4
Authors@R: c(
person("Gábor", "Csárdi", , "csardi.gabor@gmail.com",
role = c("aut", "cre")),
person(
"Brodie", "Gaslam", email="brodie.gaslam@yahoo.com",
role=c("ctb"))
)
Description: Colored terminal output on terminals that support 'ANSI'
color and highlight codes. It also works in 'Emacs' 'ESS'. 'ANSI'
color support is automatically detected. Colors and highlighting can
be combined and nested. New styles can also be created easily.
This package was inspired by the 'chalk' 'JavaScript' project.
License: MIT + file LICENSE
LazyData: true
URL: https://github.com/r-lib/crayon#readme
BugReports: https://github.com/r-lib/crayon/issues
Collate: 'ansi-256.r' 'combine.r' 'string.r' 'utils.r'
'crayon-package.r' 'disposable.r' 'has_ansi.r' 'has_color.r'
'styles.r' 'machinery.r' 'parts.r' 'print.r' 'style-var.r'
'show.r' 'string_operations.r'
Imports: grDevices, methods, utils
Suggests: mockery, rstudioapi, testthat, withr
RoxygenNote: 6.0.1.9000
Encoding: UTF-8
NeedsCompilation: no
Packaged: 2017-09-15 18:14:04 UTC; gaborcsardi
Author: Gábor Csárdi [aut, cre],
Brodie Gaslam [ctb]
Maintainer: Gábor Csárdi <csardi.gabor@gmail.com>
Repository: CRAN
Date/Publication: 2017-09-16 19:49:46 UTC
Built: R 3.5.0; ; 2018-04-23 04:01:40 UTC; unix
11 changes: 11 additions & 0 deletions rollback/testsite/golden/null-test/pkgr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Version: 1
# top level packages
Packages:
- R6
- crayon

# any repositories, order matters
Repos:
- CRAN: "https://cran.rstudio.com"

Library: "test-library"
127 changes: 127 additions & 0 deletions rollback/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package rollback

import (
"github.com/metrumresearchgroup/pkgr/desc"
"github.com/stretchr/testify/suite"
"testing"
)

type TypesTestSuite struct {
suite.Suite
}

func TestTypesTestSuite(t *testing.T) {
suite.Run(t, new(TypesTestSuite))
}

func (suite *TypesTestSuite) TestDiscernNewPackages_ToInstallCanOutnumberPreinstalled() {
crayon := desc.Desc{
Package: "crayon",
Version: "1.3.4",
Repository: "CRAN",
}

r6 := desc.Desc{
Package: "R6",
Version: "2.4.0",
Repository: "CRAN",
}

toInstallFixture := []string{"R6", "crayon", "shiny"}

preinstalledPackagesFixture := make(map[string]desc.Desc)
preinstalledPackagesFixture["crayon"] = crayon
preinstalledPackagesFixture["R6"] = r6

actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture)

suite.Equal(1, len(actual))
suite.Equal("shiny", actual[0])

}

func (suite *TypesTestSuite) TestDiscernNewPackages_AllPackagesPreinstalled() {
crayon := desc.Desc{
Package: "crayon",
Version: "1.3.4",
Repository: "CRAN",
}

r6 := desc.Desc{
Package: "R6",
Version: "2.4.0",
Repository: "CRAN",
}

toInstallFixture := []string{"R6", "crayon"}

preinstalledPackagesFixture := make(map[string]desc.Desc)
preinstalledPackagesFixture["crayon"] = crayon
preinstalledPackagesFixture["R6"] = r6

actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture)

suite.Equal(0, len(actual))
}

func (suite *TypesTestSuite) TestDiscernNewPackages_SomePackagesPreinstalled() {
crayon := desc.Desc{
Package: "crayon",
Version: "1.3.4",
Repository: "CRAN",
}

toInstallFixture := []string{"R6", "crayon"}

preinstalledPackagesFixture := make(map[string]desc.Desc)
preinstalledPackagesFixture["crayon"] = crayon

actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture)

suite.Equal(1, len(actual))
suite.Equal("R6", actual[0])
}

func (suite *TypesTestSuite) TestDiscernNewPackages_SomePackagesPreinstalled2() {
crayon := desc.Desc{
Package: "crayon",
Version: "1.3.4",
Repository: "CRAN",
}

toInstallFixture := []string{"R6", "crayon", "RColorBrewer"}

preinstalledPackagesFixture := make(map[string]desc.Desc)
preinstalledPackagesFixture["crayon"] = crayon

actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture)

suite.Equal(2, len(actual))
suite.Equal("R6", actual[0])
suite.Equal("RColorBrewer", actual[1])
}

func (suite *TypesTestSuite) TestDiscernNewPackages_PackagesAreCaseSensitive() {
crayon := desc.Desc{
Package: "CRAYON",
Version: "1.3.4",
Repository: "CRAN",
}

r6 := desc.Desc{
Package: "R6",
Version: "2.4.0",
Repository: "CRAN",
}

toInstallFixture := []string{"R6", "crayon"}

preinstalledPackagesFixture := make(map[string]desc.Desc)
preinstalledPackagesFixture["CRAYON"] = crayon
preinstalledPackagesFixture["R6"] = r6

actual := DiscernNewPackages(toInstallFixture, preinstalledPackagesFixture)

suite.Equal(1, len(actual))
suite.Equal("crayon", actual[0]) //"crayon" is considered a new package because all we can see is "CRAYON"
}
Loading

0 comments on commit 6ce29a8

Please sign in to comment.