mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-22 19:08:58 +00:00
bc27aa12cd
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `os.MkdirTemp` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
43 lines
945 B
Go
43 lines
945 B
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package model
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
)
|
|
|
|
// Test creating temporary file inside read-only directory
|
|
func TestReadOnlyDir(t *testing.T) {
|
|
// Create a read only directory, clean it up afterwards.
|
|
tmpDir := t.TempDir()
|
|
if err := os.Chmod(tmpDir, 0555); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.Chmod(tmpDir, 0755)
|
|
|
|
s := sharedPullerState{
|
|
fs: fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir),
|
|
tempName: ".temp_name",
|
|
mut: sync.NewRWMutex(),
|
|
}
|
|
|
|
fd, err := s.tempFile()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fd == nil {
|
|
t.Fatal("Unexpected nil fd")
|
|
}
|
|
|
|
s.fail(nil)
|
|
s.finalClose()
|
|
}
|