github.com/google/osv-scalibr@v0.4.1/testing/fakefs/fake_fileinfo_test.go (about)

     1  // Copyright 2025 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fakefs_test
    16  
    17  import (
    18  	"io/fs"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/google/osv-scalibr/testing/fakefs"
    23  )
    24  
    25  func TestFakeFileInfo(t *testing.T) {
    26  	tests := []struct {
    27  		desc      string
    28  		filename  string
    29  		size      int64
    30  		mode      fs.FileMode
    31  		modTime   time.Time
    32  		wantIsDir bool
    33  	}{
    34  		{
    35  			desc:      "normal file",
    36  			filename:  "test-file.txt",
    37  			size:      1024,
    38  			mode:      fs.ModePerm,
    39  			modTime:   time.Unix(1_222_333_444, 0),
    40  			wantIsDir: false,
    41  		},
    42  		{
    43  			desc:      "directory file",
    44  			filename:  "testdir",
    45  			size:      0,
    46  			mode:      fs.ModeDir,
    47  			modTime:   time.Now(),
    48  			wantIsDir: true,
    49  		},
    50  	}
    51  
    52  	for _, test := range tests {
    53  		t.Run(test.desc, func(t *testing.T) {
    54  			var gotFileInfo fs.FileInfo = fakefs.FakeFileInfo{
    55  				FileName:    test.filename,
    56  				FileSize:    test.size,
    57  				FileMode:    test.mode,
    58  				FileModTime: test.modTime,
    59  			}
    60  
    61  			if gotFileInfo.Name() != test.filename {
    62  				t.Errorf("FakeFileInfo.Name() = %q, want %q", gotFileInfo.Name(), test.filename)
    63  			}
    64  			if gotFileInfo.Size() != test.size {
    65  				t.Errorf("FakeFileInfo.Size() = %d, want %d", gotFileInfo.Size(), test.size)
    66  			}
    67  			if gotFileInfo.Mode() != test.mode {
    68  				t.Errorf("FakeFileInfo.Mode() = %v, want %v", gotFileInfo.Mode(), test.mode)
    69  			}
    70  			if !gotFileInfo.ModTime().Equal(test.modTime) {
    71  				t.Errorf("FakeFileInfo.ModTime() = %v, want %v", gotFileInfo.ModTime(), test.modTime)
    72  			}
    73  			if gotFileInfo.IsDir() != test.wantIsDir {
    74  				t.Errorf("FakeFileInfo.IsDir() = %v, want %v", gotFileInfo.IsDir(), test.wantIsDir)
    75  			}
    76  			if gotFileInfo.Sys() != nil {
    77  				t.Errorf("FakeFileInfo.Sys() = %v, want nil", gotFileInfo.Sys())
    78  			}
    79  		})
    80  	}
    81  }