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(), )
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', })
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', })
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', })
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', })
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', })
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())
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', })
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', })
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', })
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', })
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', })
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', })
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', })
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', })
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', })
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': {}, })
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', })
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', })
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', })
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', })
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', })
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': {}, })
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': {}, })
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', })