Skip to content

Commit

Permalink
added mockFunction
Browse files Browse the repository at this point in the history
Signed-off-by: satyazzz123 <beherasatyajit716@gmail.com>
  • Loading branch information
satyazzz123 committed Jan 29, 2024
1 parent bd88d39 commit 3247b4f
Showing 1 changed file with 84 additions and 25 deletions.
109 changes: 84 additions & 25 deletions environment/container/fileinfo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright IBM Corporation 2020, 2021
* Copyright IBM Corporation 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,47 +13,106 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package container

import (
"io/fs"
"testing"
"time"

"github.com/docker/docker/api/types"
)

// FileInfo implements fs.FileInfo interface
type FileInfo struct {
stat types.ContainerPathStat
func mockFunction() types.ContainerPathStat {
return types.ContainerPathStat{
Name: "mockfile.txt",
Size: int64(512),
Mode: 0755,
Mtime: time.Now(),
}
}

// Name implements fs.FileInfo interface
func (f *FileInfo) Name() string { // base name of the file
return f.stat.Name
}
func TestFileInfoName(t *testing.T) {
t.Run("Testing Name method", func(t *testing.T) {
// Use the mock function to get a types.ContainerPathStat object
realStat := mockFunction()

// Size implements fs.FileInfo interface
func (f *FileInfo) Size() int64 { // length in bytes for regular files; system-dependent for others
return f.stat.Size
// Use the real stat to create a FileInfo instance
fileInfo := &FileInfo{

Check failure on line 40 in environment/container/fileinfo.go

View workflow job for this annotation

GitHub Actions / Build and test

undefined: FileInfo
stat: realStat,
}

// Call the Name method and check if it returns the expected name
actualName := fileInfo.Name()
if actualName != realStat.Name {
t.Errorf("Name() = %s, expected %s", actualName, realStat.Name)
}
})
}

// Mode implements fs.FileInfo interface
func (f *FileInfo) Mode() fs.FileMode { // file mode bits
return f.stat.Mode
func TestFileInfoSize(t *testing.T) {
t.Run("Testing Size method", func(t *testing.T) {

realStat := mockFunction()

fileInfo := &FileInfo{

Check failure on line 57 in environment/container/fileinfo.go

View workflow job for this annotation

GitHub Actions / Build and test

undefined: FileInfo
stat: realStat,
}

// Call the Size method and check if it returns the expected size
actualSize := fileInfo.Size()
if actualSize != realStat.Size {
t.Errorf("Size() = %d, expected %d", actualSize, realStat.Size)
}
})
}

// ModTime implements fs.FileInfo interface
func (f *FileInfo) ModTime() time.Time { // modification time
return f.stat.Mtime
func TestFileInfoMode(t *testing.T) {
t.Run("Testing Mode method", func(t *testing.T) {

realStat := mockFunction()

fileInfo := &FileInfo{

Check failure on line 74 in environment/container/fileinfo.go

View workflow job for this annotation

GitHub Actions / Build and test

undefined: FileInfo
stat: realStat,
}

// Call the Mode method and check if it returns the expected mode
actualMode := fileInfo.Mode()
if actualMode != realStat.Mode {
t.Errorf("Mode() = %v, expected %v", actualMode, realStat.Mode)
}
})
}

// IsDir implements fs.FileInfo interface
func (f *FileInfo) IsDir() bool { // abbreviation for Mode().IsDir()
return f.stat.Mode.IsDir()
func TestFileInfoModTime(t *testing.T) {
t.Run("Testing ModTime method", func(t *testing.T) {

realStat := mockFunction()

fileInfo := &FileInfo{

Check failure on line 91 in environment/container/fileinfo.go

View workflow job for this annotation

GitHub Actions / Build and test

undefined: FileInfo
stat: realStat,
}

// Call the ModTime method and check if it returns the expected mod time
actualModTime := fileInfo.ModTime()
if !actualModTime.Equal(realStat.Mtime) {
t.Errorf("ModTime() = %v, expected %v", actualModTime, realStat.Mtime)
}
})
}

// Sys implements fs.FileInfo interface
func (f *FileInfo) Sys() interface{} { // underlying data source (can return nil)
return nil
func TestFileInfoIsDir(t *testing.T) {
t.Run("Testing IsDir method", func(t *testing.T) {

realStat := mockFunction()

fileInfo := &FileInfo{

Check failure on line 108 in environment/container/fileinfo.go

View workflow job for this annotation

GitHub Actions / Build and test

undefined: FileInfo
stat: realStat,
}

// Call the IsDir method and check if it returns the expected value
actualIsDir := fileInfo.IsDir()
if actualIsDir != realStat.Mode.IsDir() {
t.Errorf("IsDir() = %v, expected %v", actualIsDir, realStat.Mode.IsDir())
}
})
}

0 comments on commit 3247b4f

Please sign in to comment.