summaryrefslogtreecommitdiffstats
path: root/toolkit/components/kvstore/test/xpcshell/make-test-env.js
blob: b40632a35913c3f3a9a4b67a8d9ceaf8e818ecbc (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// An xpcshell script to create a test database.  Useful for creating
// the test-env-32 and test-env-64 databases that we use to test migration
// of databases across architecture changes.
//
// To create a test database, simply run this script using xpcshell:
//
//   path/to/xpcshell path/to/make-test-env.js
//
// The script will create the test-env-32 or test-env-64 directory
// (depending on the current architecture) in the current working directory,
// create a database called "db" within it, and populate the database
// with sample data.
//
// Note: you don't necessarily need to run this script on every architecture
// for which you'd like to create a database.  Once you have a database for one
// architecture, you can use the mdb_dump and mdb_load utilities (if available
// for your systems) to create them for others.  To do so, first dump the data
// on the original architecture:
//
//   mdb_dump -s db path/to/original/test-env-dir > path/to/dump.txt
//
// Then load the data on the new architecture:
//
//   mkdir path/to/new/test-env-dir
//   mdb_load -s db path/to/dump.txt

"use strict";

const { KeyValueService } = ChromeUtils.importESModule(
  "resource://gre/modules/kvstore.sys.mjs"
);

(async function () {
  const currentDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile).path;
  const testEnvDir = Services.appinfo.is64Bit ? "test-env-64" : "test-env-32";
  const testEnvPath = PathUtils.join(currentDir, testEnvDir);
  await IOUtils.makeDirectory(testEnvPath);

  const database = await KeyValueService.getOrCreate(testEnvPath, "db");
  await database.put("int-key", 1234);
  await database.put("double-key", 56.78);
  await database.put("string-key", "Héllo, wőrld!");
  await database.put("bool-key", true);

  scriptDone = true;
})();

// Do async processing until the async function call completes.
// From <https://developer.mozilla.org/en/XPConnect/xpcshell/HOWTO>
let scriptDone = false;
const mainThread = Services.tm.currentThread;
while (!scriptDone) {
  mainThread.processNextEvent(true);
}
while (mainThread.hasPendingEvents()) {
  mainThread.processNextEvent(true);
}