sharedpullerstate_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "os"
  18. "path/filepath"
  19. "testing"
  20. )
  21. func TestSourceFileOK(t *testing.T) {
  22. s := sharedPullerState{
  23. realName: "testdata/foo",
  24. }
  25. fd, err := s.sourceFile()
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if fd == nil {
  30. t.Fatal("Unexpected nil fd")
  31. }
  32. bs := make([]byte, 6)
  33. n, err := fd.Read(bs)
  34. if n != len(bs) {
  35. t.Fatal("Wrong read length %d != %d", n, len(bs))
  36. }
  37. if string(bs) != "foobar" {
  38. t.Fatal("Wrong contents %s != foobar", bs)
  39. }
  40. if err := s.failed(); err != nil {
  41. t.Fatal(err)
  42. }
  43. }
  44. func TestSourceFileBad(t *testing.T) {
  45. s := sharedPullerState{
  46. realName: "nonexistent",
  47. }
  48. fd, err := s.sourceFile()
  49. if err == nil {
  50. t.Fatal("Unexpected nil error")
  51. }
  52. if fd != nil {
  53. t.Fatal("Unexpected non-nil fd")
  54. }
  55. if err := s.failed(); err == nil {
  56. t.Fatal("Unexpected nil failed()")
  57. }
  58. }
  59. // Test creating temporary file inside read-only directory
  60. func TestReadOnlyDir(t *testing.T) {
  61. s := sharedPullerState{
  62. tempName: "testdata/read_only_dir/.temp_name",
  63. }
  64. // Ensure dir is read-only (git doesn't store full permissions)
  65. os.Chmod(filepath.Dir(s.tempName), 0555)
  66. fd, err := s.tempFile()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if fd == nil {
  71. t.Fatal("Unexpected nil fd")
  72. }
  73. s.earlyClose("Test done", nil)
  74. }