summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/unit/test_sourcemaps-13.js
blob: 203731fda5a9099a746cc897a741c32d9b745562 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test that we don't permanently cache source maps across reloads.
 */

var gDebuggee;
var gClient;
var gThreadClient;
var gTabClient;

const {SourceNode} = require("source-map");

function run_test()
{
  initTestDebuggerServer();
  gDebuggee = addTestGlobal("test-source-map");
  gClient = new DebuggerClient(DebuggerServer.connectPipe());
  gClient.connect().then(function () {
    attachTestTabAndResume(gClient, "test-source-map", function (aResponse, aTabClient, aThreadClient) {
      gThreadClient = aThreadClient;
      gTabClient = aTabClient;
      setup_code();
    });
  });
  do_test_pending();
}

// The MAP_FILE_NAME is .txt so that the OS will definitely have an extension ->
// content type mapping for the extension. If it doesn't (like .map or .json),
// it logs console errors, which cause the test to fail. See bug 907839.
const MAP_FILE_NAME = "temporary-generated.txt";

const TEMP_FILE_1 = "temporary1.js";
const TEMP_FILE_2 = "temporary2.js";
const TEMP_GENERATED_SOURCE = "temporary-generated.js";

function setup_code() {
  let node = new SourceNode(1, 0,
                            getFileUrl(TEMP_FILE_1, true),
                            "function temporary1() {}\n");
  let { code, map } = node.toStringWithSourceMap({
    file: getFileUrl(TEMP_GENERATED_SOURCE, true)
  });

  code += "//# sourceMappingURL=" + getFileUrl(MAP_FILE_NAME, true);
  writeFile(MAP_FILE_NAME, map.toString());

  Cu.evalInSandbox(code,
                   gDebuggee,
                   "1.8",
                   getFileUrl(TEMP_GENERATED_SOURCE, true),
                   1);

  test_initial_sources();
}

function test_initial_sources() {
  gThreadClient.getSources(function ({ error, sources }) {
    do_check_true(!error);
    sources = sources.filter(source => source.url);
    do_check_eq(sources.length, 1);
    do_check_eq(sources[0].url, getFileUrl(TEMP_FILE_1, true));
    reload(gTabClient).then(setup_new_code);
  });
}

function setup_new_code() {
  let node = new SourceNode(1, 0,
                            getFileUrl(TEMP_FILE_2, true),
                            "function temporary2() {}\n");
  let { code, map } = node.toStringWithSourceMap({
    file: getFileUrl(TEMP_GENERATED_SOURCE, true)
  });

  code += "\n//# sourceMappingURL=" + getFileUrl(MAP_FILE_NAME, true);
  writeFile(MAP_FILE_NAME, map.toString());

  gThreadClient.addOneTimeListener("newSource", test_new_sources);
  Cu.evalInSandbox(code,
                   gDebuggee,
                   "1.8",
                   getFileUrl(TEMP_GENERATED_SOURCE, true),
                   1);
}

function test_new_sources() {
  gThreadClient.getSources(function ({ error, sources }) {
    do_check_true(!error);
    sources = sources.filter(source => source.url);

    // Should now have TEMP_FILE_2 as a source.
    do_check_eq(sources.length, 1);
    let s = sources.filter(s => s.url === getFileUrl(TEMP_FILE_2, true))[0];
    do_check_true(!!s);

    finish_test();
  });
}

function finish_test() {
  do_get_file(MAP_FILE_NAME).remove(false);
  finishClient(gClient);
}