示例#1
0
 def test_yarn_init_existing_not_readable_as_file(self):
     tmpdir = mkdtemp(self)
     # Nobody expects a package.json as a directory
     os.mkdir(join(tmpdir, 'package.json'))
     os.chdir(tmpdir)
     with pretty_logging(stream=StringIO()) as stderr:
         with self.assertRaises(IOError):
             yarn.yarn_init('foo')
     self.assertIn(
         "package.json' failed; please confirm that it is a file",
         stderr.getvalue(),
     )
示例#2
0
 def test_yarn_init_existing_not_readable_as_file(self):
     tmpdir = mkdtemp(self)
     # Nobody expects a package.json as a directory
     os.mkdir(join(tmpdir, 'package.json'))
     os.chdir(tmpdir)
     with pretty_logging(stream=StringIO()) as stderr:
         with self.assertRaises(IOError):
             yarn.yarn_init('foo')
     self.assertIn(
         "package.json' failed; please confirm that it is a file",
         stderr.getvalue(),
     )
示例#3
0
    def test_yarn_init_existing_merge_interactive_yes(self):
        stub_stdouts(self)
        stub_stdin(self, 'Y')
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {
                'jquery': '~3.0.0',
                'underscore': '~1.8.0',
            }, 'devDependencies': {
                'sinon': '~1.17.0'
            }, 'name': 'dummy'}, fd, indent=0)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('foo', merge=True))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Merge results should be written when user agrees.
        self.assertEqual(result, {
            'dependencies': {
                'jquery': '~1.11.0',
                'underscore': '~1.8.0',
            },
            'devDependencies': {
                'sinon': '~1.17.0'
            },
            'name': 'foo',
        })
示例#4
0
    def test_yarn_init_existing_interactive_merge_no(self):
        stub_stdouts(self)
        stub_stdin(self, 'N')
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {
                'jquery': '~3.0.0',
                'underscore': '~1.8.0',
            }, 'devDependencies': {
                'sinon': '~1.17.0'
            }, 'name': 'dummy'}, fd, indent=0)

        os.chdir(tmpdir)
        self.assertFalse(yarn.yarn_init(
            'foo', merge=True, callback=prompt_overwrite_json))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Should not have written anything if user said no.
        self.assertEqual(result, {
            'dependencies': {
                'jquery': '~3.0.0',
                'underscore': '~1.8.0',
            },
            'devDependencies': {
                'sinon': '~1.17.0'
            },
            'name': 'dummy',
        })
示例#5
0
    def test_yarn_init_existing_overwrite(self):
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump(
                {
                    'dependencies': {
                        'jquery': '~3.0.0',
                        'underscore': '~1.8.0',
                    },
                    'devDependencies': {}
                }, fd)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('foo', overwrite=True))

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        # name wasn't already specified, so it will be automatically
        # added
        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~1.11.0'
                },
                'devDependencies': {},
                'name': 'foo',
            })
示例#6
0
    def test_yarn_init_write_name_merge(self):
        stub_stdouts(self)
        stub_stdin(self, 'Y')
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {
                'jquery': '~1.8.9',
                'underscore': '~1.8.0',
            }, 'devDependencies': {
                'sinon': '~1.17.0'
            }, 'name': 'something_else'}, fd, indent=0)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('named', merge=True))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Merge results should be written when user agrees.
        self.assertEqual(result, {
            'dependencies': {
                'jquery': '~3.0.0',
                'underscore': '~1.8.0',
            },
            'devDependencies': {
                'sinon': '~1.17.0'
            },
            # name derived from the package_json field.
            'name': 'named-js',
        })
示例#7
0
    def test_yarn_init_merge_no_overwrite_if_semantically_identical(self):
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {
                'jquery': '~1.11.0',
                'underscore': '~1.8.0',
            }, 'devDependencies': {
                'sinon': '~1.17.0'
            }, 'name': 'foo'}, fd, indent=None)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('foo', merge=True))

        with open(join(tmpdir, 'package.json')) as fd:
            # Notes that we initial wrote a file within a line with
            # explicitly no indent, so this should parse everything to
            # show that the indented serializer did not trigger.
            result = json.loads(fd.readline())

        # Merge results shouldn't have written
        self.assertEqual(result, {
            'dependencies': {
                'jquery': '~1.11.0',
                'underscore': '~1.8.0',
            },
            'devDependencies': {
                'sinon': '~1.17.0'
            },
            'name': 'foo',
        })
示例#8
0
    def test_yarn_init_existing_broken_no_overwrite_non_interactive(self):
        tmpdir = mkdtemp(self)
        # Broken json
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            fd.write('{')
        os.chdir(tmpdir)
        with pretty_logging(stream=StringIO()) as stderr:
            self.assertFalse(yarn.yarn_init('foo'))
        self.assertIn("ignoring existing malformed", stderr.getvalue())

        with open(join(tmpdir, 'package.json')) as fd:
            self.assertEqual('{', fd.read())
示例#9
0
    def test_yarn_init_existing_broken_no_overwrite_non_interactive(self):
        tmpdir = mkdtemp(self)
        # Broken json
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            fd.write('{')
        os.chdir(tmpdir)
        with pretty_logging(stream=StringIO()) as stderr:
            self.assertFalse(yarn.yarn_init('foo'))
        self.assertIn("ignoring existing malformed", stderr.getvalue())

        with open(join(tmpdir, 'package.json')) as fd:
            self.assertEqual('{', fd.read())
示例#10
0
    def test_yarn_init_new_non_interactive(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)

        self.assertTrue(yarn.yarn_init('foo'))
        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(result, {
            'dependencies': {'jquery': '~1.11.0'},
            'devDependencies': {},
            'name': 'foo',
        })
示例#11
0
    def test_yarn_init_new_multiple(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)

        self.assertTrue(
            yarn.yarn_init(['named', 'underscore']))
        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(result, {
            'dependencies': {'jquery': '~3.0.0', 'underscore': '~1.8.0'},
            'devDependencies': {},
            'name': 'underscore',
        })
示例#12
0
    def test_yarn_init_with_invalid_valid_mix(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)

        self.assertTrue(
            yarn.yarn_init(['invalid', 'underscore']))
        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(result, {
            'dependencies': {'underscore': '~1.8.0'},
            'devDependencies': {},
            'name': 'underscore',
        })
示例#13
0
    def test_yarn_init_existing_merge_overwrite(self):
        stub_stdouts(self)
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump(
                {
                    'dependencies': {
                        'jquery': '~3.0.0',
                        'underscore': '~1.8.0',
                    },
                    'devDependencies': {
                        'sinon': '~1.17.0'
                    },
                    'name': 'dummy'
                },
                fd,
                indent=0)

        os.chdir(tmpdir)
        # Overwrite will supercede interactive.
        # stub regardless, when interactive prompt failed to not trigger
        stub_stdin(self, 'n')
        self.assertTrue(
            yarn.yarn_init('foo',
                           merge=True,
                           overwrite=True,
                           callback=prompt_overwrite_json))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Merge results should be written when user agrees.
        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~1.11.0',
                    'underscore': '~1.8.0',
                },
                'devDependencies': {
                    'sinon': '~1.17.0'
                },
                'name': 'foo',
            })
示例#14
0
    def test_yarn_init_with_invalid_valid_mix(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)

        self.assertTrue(yarn.yarn_init(['invalid', 'underscore']))
        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(
            result, {
                'dependencies': {
                    'underscore': '~1.8.0'
                },
                'devDependencies': {},
                'name': 'underscore',
            })
示例#15
0
    def test_yarn_init_new_non_interactive(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)

        self.assertTrue(yarn.yarn_init('foo'))
        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~1.11.0'
                },
                'devDependencies': {},
                'name': 'foo',
            })
示例#16
0
    def test_yarn_init_new_multiple(self):
        tmpdir = mkdtemp(self)
        os.chdir(tmpdir)

        self.assertTrue(yarn.yarn_init(['named', 'underscore']))
        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~3.0.0',
                    'underscore': '~1.8.0'
                },
                'devDependencies': {},
                'name': 'underscore',
            })
示例#17
0
    def test_yarn_init_existing_broken_yes_overwrite(self):
        tmpdir = mkdtemp(self)
        # Broken json
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            fd.write('{')
        os.chdir(tmpdir)
        with pretty_logging(stream=StringIO()) as stderr:
            self.assertTrue(yarn.yarn_init('foo', overwrite=True))
        self.assertIn("ignoring existing malformed", stderr.getvalue())

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(result, {
            'dependencies': {'jquery': '~1.11.0'},
            'devDependencies': {},
            'name': 'foo',
        })
示例#18
0
    def test_yarn_init_write_name_merge(self):
        stub_stdouts(self)
        stub_stdin(self, 'Y')
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump(
                {
                    'dependencies': {
                        'jquery': '~1.8.9',
                        'underscore': '~1.8.0',
                    },
                    'devDependencies': {
                        'sinon': '~1.17.0'
                    },
                    'name': 'something_else'
                },
                fd,
                indent=0)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('named', merge=True))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Merge results should be written when user agrees.
        self.assertEqual(
            result,
            {
                'dependencies': {
                    'jquery': '~3.0.0',
                    'underscore': '~1.8.0',
                },
                'devDependencies': {
                    'sinon': '~1.17.0'
                },
                # name derived from the package_json field.
                'name': 'named-js',
            })
示例#19
0
    def test_yarn_init_existing_standard_interactive_canceled(self):
        stub_stdouts(self)
        stub_stdin(self, 'N')
        tmpdir = mkdtemp(self)
        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {}, 'devDependencies': {}}, fd)
        os.chdir(tmpdir)

        self.assertFalse(yarn.yarn_init('foo', callback=prompt_overwrite_json))

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        # Does not overwrite by default.
        self.assertEqual(result, {
            'dependencies': {},
            'devDependencies': {},
        })
示例#20
0
    def test_yarn_init_existing_interactive_merge_no(self):
        stub_stdouts(self)
        stub_stdin(self, 'N')
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump(
                {
                    'dependencies': {
                        'jquery': '~3.0.0',
                        'underscore': '~1.8.0',
                    },
                    'devDependencies': {
                        'sinon': '~1.17.0'
                    },
                    'name': 'dummy'
                },
                fd,
                indent=0)

        os.chdir(tmpdir)
        self.assertFalse(
            yarn.yarn_init('foo', merge=True, callback=prompt_overwrite_json))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Should not have written anything if user said no.
        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~3.0.0',
                    'underscore': '~1.8.0',
                },
                'devDependencies': {
                    'sinon': '~1.17.0'
                },
                'name': 'dummy',
            })
示例#21
0
    def test_yarn_init_existing_standard_interactive_canceled(self):
        stub_stdouts(self)
        stub_stdin(self, 'N')
        tmpdir = mkdtemp(self)
        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {}, 'devDependencies': {}}, fd)
        os.chdir(tmpdir)

        self.assertFalse(yarn.yarn_init('foo', callback=prompt_overwrite_json))

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        # Does not overwrite by default.
        self.assertEqual(result, {
            'dependencies': {},
            'devDependencies': {},
        })
示例#22
0
    def test_yarn_init_existing_merge_interactive_yes(self):
        stub_stdouts(self)
        stub_stdin(self, 'Y')
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump(
                {
                    'dependencies': {
                        'jquery': '~3.0.0',
                        'underscore': '~1.8.0',
                    },
                    'devDependencies': {
                        'sinon': '~1.17.0'
                    },
                    'name': 'dummy'
                },
                fd,
                indent=0)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('foo', merge=True))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Merge results should be written when user agrees.
        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~1.11.0',
                    'underscore': '~1.8.0',
                },
                'devDependencies': {
                    'sinon': '~1.17.0'
                },
                'name': 'foo',
            })
示例#23
0
    def test_yarn_init_existing_broken_yes_overwrite(self):
        tmpdir = mkdtemp(self)
        # Broken json
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            fd.write('{')
        os.chdir(tmpdir)
        with pretty_logging(stream=StringIO()) as stderr:
            self.assertTrue(yarn.yarn_init('foo', overwrite=True))
        self.assertIn("ignoring existing malformed", stderr.getvalue())

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~1.11.0'
                },
                'devDependencies': {},
                'name': 'foo',
            })
示例#24
0
    def test_yarn_init_merge_no_overwrite_if_semantically_identical(self):
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump(
                {
                    'dependencies': {
                        'jquery': '~1.11.0',
                        'underscore': '~1.8.0',
                    },
                    'devDependencies': {
                        'sinon': '~1.17.0'
                    },
                    'name': 'foo'
                },
                fd,
                indent=None)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('foo', merge=True))

        with open(join(tmpdir, 'package.json')) as fd:
            # Notes that we initial wrote a file within a line with
            # explicitly no indent, so this should parse everything to
            # show that the indented serializer did not trigger.
            result = json.loads(fd.readline())

        # Merge results shouldn't have written
        self.assertEqual(
            result, {
                'dependencies': {
                    'jquery': '~1.11.0',
                    'underscore': '~1.8.0',
                },
                'devDependencies': {
                    'sinon': '~1.17.0'
                },
                'name': 'foo',
            })
示例#25
0
    def test_yarn_init_existing_overwrite(self):
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {
                'jquery': '~3.0.0',
                'underscore': '~1.8.0',
            }, 'devDependencies': {}}, fd)

        os.chdir(tmpdir)
        self.assertTrue(yarn.yarn_init('foo', overwrite=True))

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        # name wasn't already specified, so it will be automatically
        # added
        self.assertEqual(result, {
            'dependencies': {'jquery': '~1.11.0'},
            'devDependencies': {},
            'name': 'foo',
        })
示例#26
0
    def test_yarn_init_existing_standard_non_interactive(self):
        tmpdir = mkdtemp(self)

        # Write an initial thing
        target = join(tmpdir, 'package.json')
        with open(target, 'w') as fd:
            json.dump({'dependencies': {}, 'devDependencies': {}}, fd)

        os.chdir(tmpdir)

        with pretty_logging(stream=StringIO()) as stderr:
            self.assertFalse(yarn.yarn_init('foo'))
            self.assertIn(
                "not overwriting existing '%s'" % target, stderr.getvalue())

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        # Does not overwrite by default.
        self.assertEqual(result, {
            'dependencies': {},
            'devDependencies': {},
        })
示例#27
0
    def test_yarn_init_existing_standard_non_interactive(self):
        tmpdir = mkdtemp(self)

        # Write an initial thing
        target = join(tmpdir, 'package.json')
        with open(target, 'w') as fd:
            json.dump({'dependencies': {}, 'devDependencies': {}}, fd)

        os.chdir(tmpdir)

        with pretty_logging(stream=StringIO()) as stderr:
            self.assertFalse(yarn.yarn_init('foo'))
            self.assertIn("not overwriting existing '%s'" % target,
                          stderr.getvalue())

        with open(join(tmpdir, 'package.json')) as fd:
            result = json.load(fd)

        # Does not overwrite by default.
        self.assertEqual(result, {
            'dependencies': {},
            'devDependencies': {},
        })
示例#28
0
    def test_yarn_init_existing_merge_overwrite(self):
        stub_stdouts(self)
        tmpdir = mkdtemp(self)

        # Write an initial thing
        with open(join(tmpdir, 'package.json'), 'w') as fd:
            json.dump({'dependencies': {
                'jquery': '~3.0.0',
                'underscore': '~1.8.0',
            }, 'devDependencies': {
                'sinon': '~1.17.0'
            }, 'name': 'dummy'}, fd, indent=0)

        os.chdir(tmpdir)
        # Overwrite will supercede interactive.
        # stub regardless, when interactive prompt failed to not trigger
        stub_stdin(self, 'n')
        self.assertTrue(yarn.yarn_init(
            'foo', merge=True, overwrite=True, callback=prompt_overwrite_json))

        with open(join(tmpdir, 'package.json')) as fd:
            with self.assertRaises(ValueError):
                json.loads(fd.readline())
            fd.seek(0)
            result = json.load(fd)

        # Merge results should be written when user agrees.
        self.assertEqual(result, {
            'dependencies': {
                'jquery': '~1.11.0',
                'underscore': '~1.8.0',
            },
            'devDependencies': {
                'sinon': '~1.17.0'
            },
            'name': 'foo',
        })