simple tool to de-duplicate and arrange media.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
6.8 KiB

5 years ago
5 years ago
5 years ago
  1. package arrange
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "time"
  9. )
  10. func TestFileMove(t *testing.T) {
  11. wd, err := os.Getwd()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. tmp, err := ioutil.TempDir("", "arrange-tests-")
  16. if err != nil {
  17. t.Fatal(err)
  18. }
  19. defer func() {
  20. os.RemoveAll(tmp)
  21. }()
  22. if err := PrepOutput(tmp); err != nil {
  23. t.Fatal(err)
  24. }
  25. tests := []struct {
  26. path string
  27. expected []string
  28. ts time.Time
  29. }{
  30. {
  31. path: filepath.Join(wd, "testdata", "lenna.png"),
  32. expected: []string{
  33. "date/2012/10/1350815400000000000.png",
  34. "content/81/4a0034f5549e957ee61360d87457e5.png",
  35. },
  36. ts: time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
  37. },
  38. {
  39. path: filepath.Join(wd, "testdata", "valid.jpg"),
  40. expected: []string{
  41. "date/2012/10/1350815400000000001.jpg",
  42. "content/77/d2a6bf840622331df62963174df72d.jpg",
  43. },
  44. ts: time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
  45. },
  46. {
  47. path: filepath.Join(wd, "testdata", "stott.gif"),
  48. expected: []string{
  49. "date/2012/10/1350815400000000001.gif",
  50. "content/9e/f476f6e1ee9ecf64ca443eb63e4655.gif",
  51. },
  52. ts: time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
  53. },
  54. {
  55. path: filepath.Join(wd, "testdata", "exif-decode-error.jpg"),
  56. expected: []string{
  57. "date/2006/03/1143508767000000000.jpg",
  58. "content/4e/08875f21249becbc18bc978b35a9b4.jpg",
  59. },
  60. ts: time.Date(2012, 10, 21, 10, 30, 0, 2, time.UTC),
  61. },
  62. {
  63. path: filepath.Join(wd, "testdata", "no-exif-but-good.jpg"),
  64. expected: []string{
  65. "date/2012/10/1350815400000000003.jpg",
  66. "content/a2/7735c61b375da9012a33dbdb548274.jpg",
  67. },
  68. ts: time.Date(2012, 10, 21, 10, 30, 0, 3, time.UTC),
  69. },
  70. {
  71. path: filepath.Join(wd, "testdata", "bad.mov"),
  72. expected: []string{
  73. "date/2012/10/1350815400000000004.mov",
  74. "content/d4/1d8cd98f00b204e9800998ecf8427e.mov",
  75. },
  76. ts: time.Date(2012, 10, 21, 10, 30, 0, 4, time.UTC),
  77. },
  78. }
  79. for _, test := range tests {
  80. if err := os.Chtimes(test.path, test.ts, test.ts); err != nil {
  81. t.Fatalf("chtime fail: %v", err)
  82. }
  83. m, err := _parse(test.path)
  84. if err != nil {
  85. t.Fatalf("problem parsing known good png: %v", err)
  86. }
  87. if err := m.Move(tmp); err != nil {
  88. t.Fatalf("problem moving file into place: %v", err)
  89. }
  90. for _, p := range test.expected {
  91. if _, err := os.Stat(filepath.Join(tmp, p)); os.IsNotExist(err) {
  92. t.Errorf("could not find expected file %q: %v", p, err)
  93. }
  94. }
  95. }
  96. }
  97. func TestBadFiles(t *testing.T) {
  98. wd, err := os.Getwd()
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. tmp, err := ioutil.TempDir("", "arrange-tests-")
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. defer func() {
  107. os.RemoveAll(tmp)
  108. }()
  109. if err := PrepOutput(tmp); err != nil {
  110. t.Fatal(err)
  111. }
  112. tests := []struct {
  113. path string
  114. }{
  115. {
  116. path: filepath.Join(wd, "testdata", "bad-link"),
  117. },
  118. {
  119. path: filepath.Join(wd, "testdata", "not.a.jpg"),
  120. },
  121. {
  122. path: filepath.Join(wd, "testdata", "not.a.png"),
  123. },
  124. {
  125. path: filepath.Join(wd, "testdata", "too-many-links.jpg"),
  126. },
  127. }
  128. for _, test := range tests {
  129. _, err := _parse(test.path)
  130. if err == nil {
  131. t.Fatalf("should have had an error in parse of %q, got nil", test.path)
  132. }
  133. }
  134. }
  135. func TestMoveCollision(t *testing.T) {
  136. wd, err := os.Getwd()
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. tmp, err := ioutil.TempDir("", "arrange-tests-")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. defer func() {
  145. os.RemoveAll(tmp)
  146. }()
  147. if err := PrepOutput(tmp); err != nil {
  148. t.Fatal(err)
  149. }
  150. ts := time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC)
  151. media := []Media{
  152. Media{
  153. Path: filepath.Join(wd, "testdata", "a.mov"),
  154. Hash: "60b725f10c9c85c70d97880dfe8191b3",
  155. Time: ts,
  156. Extension: ".mov",
  157. },
  158. Media{
  159. Path: filepath.Join(wd, "testdata", "b.mov"),
  160. Hash: "3b5d5c3712955042212316173ccf37be",
  161. Time: ts,
  162. Extension: ".mov",
  163. },
  164. Media{
  165. Path: filepath.Join(wd, "testdata", "c.mov"),
  166. Hash: "2cd6ee2c70b0bde53fbe6cac3c8b8bb1",
  167. Time: ts,
  168. Extension: ".mov",
  169. },
  170. }
  171. for _, m := range media {
  172. if err := m.Move(tmp); err != nil {
  173. t.Fatalf("move: %v", err)
  174. }
  175. }
  176. expected := []string{
  177. "date/2012/10/1350815400000000000_0001.mov",
  178. "date/2012/10/1350815400000000000_0000.mov",
  179. "date/2012/10/1350815400000000000.mov",
  180. }
  181. for _, p := range expected {
  182. if _, err := os.Stat(filepath.Join(tmp, p)); os.IsNotExist(err) {
  183. t.Errorf("could not find expected file %q: %v", p, err)
  184. }
  185. }
  186. }
  187. func TestSundry(t *testing.T) {
  188. _ = fmt.Sprintf("%v", NotMedia{"hi"})
  189. _ = fmt.Sprintf("%v", Dup{"hi"})
  190. }
  191. func TestFlow(t *testing.T) {
  192. wd, err := os.Getwd()
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. tmp, err := ioutil.TempDir("", "arrange-tests-")
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. defer func() {
  201. os.RemoveAll(tmp)
  202. }()
  203. if err := PrepOutput(tmp); err != nil {
  204. t.Fatal(err)
  205. }
  206. files := []struct {
  207. path string
  208. ts time.Time
  209. }{
  210. {
  211. path: filepath.Join(wd, "testdata", "a.mov"),
  212. ts: time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
  213. },
  214. {
  215. path: filepath.Join(wd, "testdata", "b.mov"),
  216. ts: time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
  217. },
  218. {
  219. path: filepath.Join(wd, "testdata", "c.mov"),
  220. ts: time.Date(2012, 10, 21, 10, 30, 0, 2, time.UTC),
  221. },
  222. {
  223. path: filepath.Join(wd, "testdata", "bad.mov"),
  224. ts: time.Date(2012, 10, 21, 10, 30, 0, 3, time.UTC),
  225. },
  226. {
  227. path: filepath.Join(wd, "testdata", "no-exif-but-good.jpg"),
  228. ts: time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
  229. },
  230. {
  231. path: filepath.Join(wd, "testdata", "exif-decode-error.jpg"),
  232. ts: time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
  233. },
  234. {
  235. path: filepath.Join(wd, "testdata", "valid.jpg"),
  236. ts: time.Date(2012, 10, 21, 10, 30, 0, 2, time.UTC),
  237. },
  238. {
  239. path: filepath.Join(wd, "testdata", "stott.gif"),
  240. ts: time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
  241. },
  242. {
  243. path: filepath.Join(wd, "testdata", "lenna.png"),
  244. ts: time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
  245. },
  246. }
  247. for _, f := range files {
  248. if err := os.Chtimes(f.path, f.ts, f.ts); err != nil {
  249. t.Errorf("failure to chtime: %v", err)
  250. }
  251. }
  252. work := Source(filepath.Join(wd, "testdata"))
  253. streams := []<-chan Media{}
  254. for w := 0; w < 4; w++ {
  255. streams = append(streams, Parse(work))
  256. }
  257. for err := range Move(Merge(streams), tmp) {
  258. if err != nil {
  259. t.Errorf("unexpected error: %v", err)
  260. }
  261. }
  262. expected := []string{
  263. "date/2012/10/1350815400000000000.mov",
  264. "date/2012/10/1350815400000000001.mov",
  265. "date/2012/10/1350815400000000002.mov",
  266. "date/2012/10/1350815400000000003.mov",
  267. "date/2012/10/1350815400000000000.jpg",
  268. "date/2012/10/1350815400000000002.jpg",
  269. // this one has valid exif
  270. "date/2006/03/1143508767000000000.jpg",
  271. "date/2012/10/1350815400000000000.gif",
  272. "date/2012/10/1350815400000000000.png",
  273. }
  274. for _, p := range expected {
  275. if _, err := os.Stat(filepath.Join(tmp, p)); os.IsNotExist(err) {
  276. t.Errorf("could not find expected file %q: %v", p, err)
  277. }
  278. }
  279. }