summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_unique.js
blob: 740d84a2d6550e3dab0249ee20d8fb87b6fb4624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict";

const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");

function run_test() {
  do_get_profile();
  run_next_test();
}

function testFiles(filename) {
  return (async function() {
    const MAX_TRIES = 10;
    let profileDir = OS.Constants.Path.profileDir;
    let path = OS.Path.join(profileDir, filename);

    // Ensure that openUnique() uses the file name if there is no file with that name already.
    let openedFile = await OS.File.openUnique(path);
    info("\nCreate new file: " + openedFile.path);
    await openedFile.file.close();
    let exists = await OS.File.exists(openedFile.path);
    Assert.ok(exists);
    Assert.equal(path, openedFile.path);
    let fileInfo = await OS.File.stat(openedFile.path);
    Assert.ok(fileInfo.size == 0);

    // Ensure that openUnique() creates a new file name using a HEX number, as the original name is already taken.
    openedFile = await OS.File.openUnique(path);
    info("\nCreate unique HEX file: " + openedFile.path);
    await openedFile.file.close();
    exists = await OS.File.exists(openedFile.path);
    Assert.ok(exists);
    fileInfo = await OS.File.stat(openedFile.path);
    Assert.ok(fileInfo.size == 0);

    // Ensure that openUnique() generates different file names each time, using the HEX number algorithm
    let filenames = new Set();
    for (let i = 0; i < MAX_TRIES; i++) {
      openedFile = await OS.File.openUnique(path);
      await openedFile.file.close();
      filenames.add(openedFile.path);
    }

    Assert.equal(filenames.size, MAX_TRIES);

    // Ensure that openUnique() creates a new human readable file name using, as the original name is already taken.
    openedFile = await OS.File.openUnique(path, { humanReadable: true });
    info("\nCreate unique Human Readable file: " + openedFile.path);
    await openedFile.file.close();
    exists = await OS.File.exists(openedFile.path);
    Assert.ok(exists);
    fileInfo = await OS.File.stat(openedFile.path);
    Assert.ok(fileInfo.size == 0);

    // Ensure that openUnique() generates different human readable file names each time
    filenames = new Set();
    for (let i = 0; i < MAX_TRIES; i++) {
      openedFile = await OS.File.openUnique(path, { humanReadable: true });
      await openedFile.file.close();
      filenames.add(openedFile.path);
    }

    Assert.equal(filenames.size, MAX_TRIES);

    let exn;
    try {
      for (let i = 0; i < 100; i++) {
        openedFile = await OS.File.openUnique(path, { humanReadable: true });
        await openedFile.file.close();
      }
    } catch (ex) {
      exn = ex;
    }

    info("Ensure that this raises the correct error");
    Assert.ok(!!exn);
    Assert.ok(exn instanceof OS.File.Error);
    Assert.ok(exn.becauseExists);
  })();
}

add_task(async function test_unique() {
  OS.Shared.DEBUG = true;
  // Tests files with extension
  await testFiles("dummy_unique_file.txt");
  // Tests files with no extension
  await testFiles("dummy_unique_file_no_ext");
});