summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/test/qa-tests/jstests/common/check_version.js
blob: cf151e41e75a8f53c280754ce95df1a33ff943d4 (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
/**
 *  Given a MongoDB version, parses it into its major/minor/patch components,
 *  discounting '-pre' and '-rcX'. Useful for parsing the output of
 *  `db.version()` into an appropriate form for comparisons.
 *
 *  Examples:
 *    getVersionComponents('2.7.8'); // { major: 2, minor: 7, patch: 8 }
 *    getVersionComponents('2.8.0-rc0'); // { major: 2, minor: 8, patch: 0 }
 */
var getVersionComponents = function(version) {
  var splitVersion = version.split('.');
  assert.eq(3, splitVersion.length);
  var major = parseInt(splitVersion[0], 10);
  var minor = parseInt(splitVersion[1], 10);

  var patchEnd = splitVersion[2].indexOf('-') !== -1 ?
    splitVersion[2].indexOf('-') :
    undefined;
  var patch = parseInt(splitVersion[2].substr(0, patchEnd));
  return {
    major: major,
    minor: minor,
    patch: patch,
  };
};

/**
 *  Given two versions, returns true if the first version is >= the second.
 *
 *  Examples:
 *    isAtLeastVersion('2.7.8', '2.7.8'); // true
 *    isAtLeastVersion('2.8.0-rc0', '2.7.8'); // true
 *    isAtLeastVersion('2.6.6', '2.7.8'); // false
 *    isAtLeastVersion('1.8.5', '2.7.8'); // false
 */
/* exported isAtLeastVersion */
var isAtLeastVersion = function(serverVersion, checkVersion) {
  serverVersion = getVersionComponents(serverVersion);
  checkVersion = getVersionComponents(checkVersion);

  return (checkVersion.major < serverVersion.major) ||
    (checkVersion.major === serverVersion.major &&
      checkVersion.minor < serverVersion.minor) ||
    (checkVersion.major === serverVersion.major &&
      checkVersion.minor === serverVersion.minor &&
      checkVersion.patch <= serverVersion.patch);
};