1// Copyright (C) 2013-2015 Sören Tempel2//3// This program is free software: you can redistribute it and/or modify4// it under the terms of the GNU General Public License as published by5// the Free Software Foundation, either version 3 of the License, or6// (at your option) any later version.7//8// This program is distributed in the hope that it will be useful,9// but WITHOUT ANY WARRANTY; without even the implied warranty of10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11// GNU General Public License for more details.12//13// You should have received a copy of the GNU General Public License14// along with this program. If not, see <http://www.gnu.org/licenses/>.1516package util1718import (19 "io/ioutil"20 "os"21 "path/filepath"22 "testing"23)2425func TestLock1(t *testing.T) {26 file, err := ioutil.TempFile(os.TempDir(), "testLock")27 if err != nil {28 t.Fatal(err)29 }3031 defer file.Close()32 fi, err := file.Stat()33 if err != nil {34 t.Fatal(err)35 }3637 lockPath := filepath.Join(os.TempDir(), fi.Name())38 if err := Lock(lockPath); !os.IsExist(err) {39 t.Fail()40 }4142 if err := os.Remove(lockPath); err != nil {43 t.Fatal(err)44 }45}4647func TestLock2(t *testing.T) {48 lockPath := filepath.Join(os.TempDir(), "lockTest")49 if err := Lock(lockPath); os.IsExist(err) {50 t.Fatal(err)51 }5253 if err := os.Remove(lockPath); err != nil {54 t.Fatal(err)55 }56}5758func TestEscape(t *testing.T) {59 type testpair struct {60 unescaped string61 escaped string62 }6364 tests := []testpair{65 {"$$foo /", "foo"},66 {"Foo bar, baz!", "Foo-bar-baz"},67 {"LNP007: Foobar!", "LNP007-Foobar"},68 {"B$:(=== >$-%)/A/R", "B-A-R"},69 {"foobar ", "foobar"},70 }7172 for _, test := range tests {73 e, err := Escape(test.unescaped)74 if err != nil {75 t.Fatal(err)76 }7778 if e != test.escaped {79 t.Fatalf("Expected %q - got %q", test.escaped, e)80 }81 }82}8384func TestEnvDefault1(t *testing.T) {85 if err := os.Setenv("TESTDIR", "/foo"); err != nil {86 t.Fatal(err)87 }8889 dir := EnvDefault("TESTDIR", "")90 if dir != "/foo" {91 t.Fatalf("Expected %q - got %q", "/foo", dir)92 }93}9495func TestEnvDefault2(t *testing.T) {96 dir := EnvDefault("TESTDIR2", "bar")97 if dir != filepath.Join(os.Getenv("HOME"), "bar") {98 t.Fatalf("Expected %q - got %q", filepath.Join(os.Getenv("HOME"), "bar"), dir)99 }100}