2022-09-24 08:04:14 -05:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-09-24 08:04:14 -05:00
|
|
|
|
|
|
|
//go:build !windows
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 14:41:10 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-09-24 08:04:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestApplyUmask(t *testing.T) {
|
|
|
|
f, err := os.CreateTemp(t.TempDir(), "test-filemode-")
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2022-09-24 08:04:14 -05:00
|
|
|
|
|
|
|
err = os.Chmod(f.Name(), 0o777)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2022-09-24 08:04:14 -05:00
|
|
|
st, err := os.Stat(f.Name())
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2022-09-24 08:04:14 -05:00
|
|
|
assert.EqualValues(t, 0o777, st.Mode().Perm()&0o777)
|
|
|
|
|
|
|
|
oldDefaultUmask := defaultUmask
|
|
|
|
defaultUmask = 0o037
|
|
|
|
defer func() {
|
|
|
|
defaultUmask = oldDefaultUmask
|
|
|
|
}()
|
|
|
|
err = ApplyUmask(f.Name(), os.ModePerm)
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2022-09-24 08:04:14 -05:00
|
|
|
st, err = os.Stat(f.Name())
|
2024-07-30 14:41:10 -05:00
|
|
|
require.NoError(t, err)
|
2022-09-24 08:04:14 -05:00
|
|
|
assert.EqualValues(t, 0o740, st.Mode().Perm()&0o777)
|
|
|
|
}
|